Hello , Can you Tell me what is sleep Funtion in Javascript , and How to Implement Sleep Function , And what are other Necessary Constraint Related to it , Please Help Thanks in advance
Hello , Can you Tell me what is sleep Funtion in Javascript , and How to Implement Sleep Function , And what are other Necessary Constraint Related to it , Please Help Thanks in advance
Check the implementation of the "sleep" function in javascript.
Code:function sleep(timeout) { var loop = true; var current = new Date(); var now; var cTimestamp = current.getTime(); while(loop) { now = new Date(); nTimestamp = now.getTime(); if(nTimestamp - cTimestamp > timeout) { loop = false; } } }
the only possible solution seems to be currently going through the narrative javascript compiler that will compile a code that will allow us to make pauses in it.
function sleep (millis) (
var notify = new EventNotifier ();
setTimeout (notifier, millis);
notified. wait -> ();
)
the Truth is the pause in javascript is not recommended beyond the fact that it is not really feasible. I just wrote this article because I needed to test the function I created evalglobal under the engine KHTML (Konquero, Safari ..). I thought going through setTimeout departure, which was a mistake. try this on any engine, the result is impressive:
The execution priority is given to the current scope which leaves the function sent to setTimeout.Code:test = undefined; setTimeout ( "var test = function () (alert (" test is a function ");) ', 10); var Start = (new Date ()). GetTime (); while (typeof test! = 'function') ( if (((new Date ()). GetTime () - start)> 6000) ( alert ( 'test definitely undefined'); break; ) ) if (typeof test == 'function') test ();
Bookmarks