Is it bad practice to pass a string to settimeout? If yes, why? [duplicate]
Yes, it is. The string must be eval‘d, which is evil (and very slow). It also prevents you from using local variables in the callback.
Yes, it is. The string must be eval‘d, which is evil (and very slow). It also prevents you from using local variables in the callback.
setTimeout and such aren’t in the ECMAScript specification because they’re not JavaScript features. They’re features of the browser environment’s window object. Other environments (Windows Scripting Host, NodeJS, etc.) won’t necessarily have those features. The W3C has been trying to standardize the window object and its various features (including setTimeout), the latest is in the timers … Read more
The while loop will not wait for setTimeout() to complete. You need to set different time delay for each to execute them with different times and use closure for holding the value of i. Also in your case, function will be executed initially and return value is setting as argument in setTimeout(), so either you … Read more
There’s no actual recursion because the call to GetData is delayed and the JavaScript context is destroyed in the meantime. So it won’t crash the JS engine. For your code sample, this is basically what will happen at the JS engine level: Initialize JS engine Create GetData function context Execute GetData statements including “setTimeOut” “setTimeOut” … Read more
You can break .delay() by .dequeue() function here is example //this is only for make sure that we skip ‘delay’, not other function var inDelay = false; function start() { $(‘.gfx’).animate ({ width: 100 }, function(){inDelay = true}).delay(3000).animate ({ width: 0 }, function(){inDelay = false}) } function breakTheDelay() { if(inDelay) { $(‘.gfx’).dequeue(); } } http://jsfiddle.net/wasikuss/5288z/ … Read more
param in Internet explorer specifies whether the code in myFunction is JScript, JavaScript or VBscript See also: MSDN. It does not behave like other browsers. The following will work: setTimeout(function() { myFunction(param); }, 1000); The previous line does not exactly mimic setTimeout in Firefox etc. To pass a variable, unaffected by a later update to … Read more
tl;dr: Don’t worry about the cost of timers until you’re creating 100K’s of them. I just did a quick test of timer performance by creating this test file (creates 100K timers over and over): <script> var n = 0; // Counter used to verify all timers fire function makeTimers() { var start = Date.now(); for … Read more
There is no way to delay execution of part of the code of in the current script. It wouldn’t make much sense, either, as the processing of a PHP script takes place entirely on server side and you would just delay the overall execution of the script. There is sleep() but that will simply halt … Read more
Very simplified: Browsers are single threaded and this single thread (The UI thread) is shared between the rendering engine and the js engine. If the thing you want to do takes alot of time (we talking cycles here but still) it could halt (paus) the rendering (flow and paint). In browsers there also exists “The … Read more
What I do is: var timer = null; if (timer != null) { window.clearTimeout(timer); timer = null; } else { timer = window.setTimeout(yourFunction, 0); }