The custom method implementation in js stays for a few seconds to sleep

  • 2020-03-30 03:31:56
  • OfStack

Js does not exist its own sleep method, to want to sleep to define their own method


function sleep(numberMillis) { 
var now = new Date(); 
var exitTime = now.getTime() + numberMillis; 
while (true) { 
now = new Date(); 
if (now.getTime() > exitTime) 
return; 
} 
}

Here are the additions:

In addition to the Narrative JS, (link: http://chumsley.org/jwacs/index.html) is also committed to by extending the JavaScript syntax to avoid writing a headache asynchronous calls the callback function. Sleep with jwacs, the code is as follows:


function sleep(msec) {
    var k = function_continuation;
    setTimeout(function() { resume k <- mesc; }, msec);
    suspend;
}

This syntax is even scarier and is the name of a thread method that is not recommended in Java. Frankly, I'm leaning towards Narrative JS.

Like Narrative JS, jwacs requires precompilation, which is written in LISP. It is also currently available in Alpha. More introduction and comparison of both can refer to the new article on the SitePoint: Eliminating async Javascript callbacks by preprocessing

As we all know, JavaScript does not provide the same thread-control functionality as Java, and although setTimeout and setInterval are available to do some timed execution control, they do not meet all the requirements. There have been a lot of questions about how to implement sleep/pause/wait in JavaScript, and there have been some really bad solutions:

The easiest and worst way to do this is to write a loop that looks like this:


function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

The code above doesn't actually put the script interpreter to sleep, and it has the added effect of quickly loading the CPU. The browser will even be in suspended animation for that period of time.

Second, there is a smart person to use IE special dialog box to achieve the winding path, the code may be as follows:


function sleep(timeout) {
 window.showModalDialog("javascript:document.writeln('<script>window.setTimeout(function () { window.close(); }, " + timeout + ");</script>');");
}window.alert("before sleep ...");
sleep(2000);
window.alert("after sleep ...");

In addition to the above, there is the idea of using applets or calling Windows Script Host's wscript.sleep (), and so on, as a last resort.

Finally, someone smarter has developed what may be the best solution. Let's look at the code first:


function sleep(millis) {
    var notifier = NjsRuntime.createNotifier();
    setTimeout(notifier, millis);
    notifier.wait->();
}

Yes, see -> The syntax of (), like I just saw Prototype's $() function, surprised me. However, this script will report syntax errors directly in the browser. They actually need to be precompiled into JavaScript that the client browser approves of. The compiled script is as follows:


function sleep(millis){
var njf1 = njen(this,arguments,"millis");
nj:while(1) {
try{switch(njf1.cp) {
case 0:njf1._notifier=NjsRuntime.createNotifier();
setTimeout(njf1._notifier,njf1._millis);
njf1.cp = 1;
njf1._notifier.wait(njf1);
return;
case 1:break nj;
}} catch(ex) {
if(!njf1.except(ex,1))
return;
}}
njf1.pf();
}

Buggy zigzag implementation


<script type"text/javascript">

function Pause(obj,iMinSecond){
 if (window.eventList==null) window.eventList=new Array();
 var ind=-1;
 for (var i=0;i<window.eventList.length;i++){
 if (window.eventList[i]==null) {
  window.eventList[i]=obj;
  ind=i;
  break;
 }
 }
 
 if (ind==-1){
 ind=window.eventList.length;
 window.eventList[ind]=obj;
 }
 setTimeout("GoOn(" + ind + ")",1000);
}


function GoOn(ind){
 var obj=window.eventList[ind];
 window.eventList[ind]=null;
 if (obj.NextStep) obj.NextStep();
 else obj();
}

function Test(){
 alert("hellow");
 Pause(this,1000);//Call the pause function
 this.NextStep=function(){
 alert("NextStep");
 }
}
</script>

Related articles: