Combination of async function + await + setTimeout

Your sleep function does not work because setTimeout does not (yet?) return a promise that could be awaited. You will need to promisify it manually: function timeout(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function sleep(fn, …args) { await timeout(3000); return fn(…args); } Btw, to slow down your loop you probably don’t want … Read more

Pass correct “this” context to setTimeout callback?

EDIT: In summary, back in 2010 when this question was asked the most common way to solve this problem was to save a reference to the context where the setTimeout function call is made, because setTimeout executes the function with this pointing to the global object: var that = this; if (this.options.destroyOnHide) { setTimeout(function(){ that.tip.destroy() … Read more

How can I pass a parameter to a setTimeout() callback?

setTimeout(function() { postinsql(topicId); }, 4000) You need to feed an anonymous function as a parameter instead of a string, the latter method shouldn’t even work per the ECMAScript specification but browsers are just lenient. This is the proper solution, don’t ever rely on passing a string as a ‘function’ when using setTimeout() or setInterval(), it’s … Read more

Why is set.timeout not working in this php while loop?

You cannot really use the setTimeout() function as you suggest… I guess this is roughly what you are looking for: echo <<< EOT <script type=”text/javascript”> setTimeout(function() { window.open(‘https://mywebsite/$link’, ‘_blank’); }, 1000); </script> EOT; Note: I just use the nowdoc notation since it is easier to read. Certainly it is possible to use a normal literal … Read more