Recently in the use of setinterval in the timer, found some problems.
That is, once the timer has run out of time, JavaScript does not wait for the task to complete, to reproduce the calculation interval, but to immediately add the next task to the queue at the time interval, and wait for the task to execute immediately, All timed loads become loop loaded. That's what we don't want to say.
SetInterval Code:
FUNCTION&NBSP;STARTFN2 () { var p2 = new Alarmclockbyinterval (callbackbytest, 2000);} Function callbackbytest () { var i = 0; for (; i < 900000000; i++) { } Return true;} Function alarmclockbyinterval (_ARGS1,&NBSP;_ARGS2) { var timeFn, self = this, callbackfn = _args1, ms = _args2, i = 0; this.getinterval = function () { if (MS) { if (!TIMEFN) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; timefn = setinterval (function () { console.log ("Timed Tasks start executing:" +new date (). GetTime ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CALLBACKFN (); Console.log ("Timed Task End execution:" +new date (). GetTime ()); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;},&NBSP;MS); } } else { &nbsP; closeinterval (); } } this.closeinterval = function () { if (TIMEFN) { clearinterval (TIMEFN); } } self.getinterval ();}
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/78/5C/wKiom1Z6dhmhhy5XAABKJcZDauc152.png "title=" execution result " alt= "Wkiom1z6dhmhhy5xaabkjczdauc152.png"/>
As you can see, once the scheduled task is completed, the next task is immediately performed and there is no ideal interval.
The task execution time is also calculated to a time interval. Direct use of setinterval does not achieve our desired results.
Solve:
Using settimeout, and recursion, using the function to automatically call itself, very late execution can be a good solution to this problem.
Function alarmclockbytimeout (_ARGS1,&NBSP;_ARGS2) { var _type = 0, timeFn, _flag = true, ms = _args2, callbackfn = _args1, self = this; this.gettimeout = function () { var _ callee = arguments.callee; if (_flag) { //internal error, internal forced interrupt, terminating recursion if (_type == 0) { //external termination recursion timefn = settimeout (function () { &Nbsp; console.log ("Timed Tasks start executing:" +new date (). GetTime ()); &NBSP;&NBSP;_FLAG&NBSP;=&NBSP;CALLBACKFN (); console.log ("Timed Task End execution:" +new date (). GetTime ()); _callee (); &NBSP;&NBSP;},&NBSP;MS); } else { if (TIMEFN) cleartimeout (timefn); consoLe.error (500, "timer terminated, external termination ..."); } } else { if (Timefn) cleartimeout (timefn); console.error (500, "timer terminated, callback function error or internal forced termination ..."); } }; this.close = function (_ARGS1) { _type = _args1 | | 1; }; self.gettimeout ();} FUNCTION&NBSP;STARTFN2 () { var p1 = new alarmclockbytimeout ( callbackbytest,1000); // var p2 = new alarmclockbyinterval ( callbackbytest, 2000);}
Execution effect:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/78/5B/wKioL1Z6d9-QrBqsAABa2wJuz8Y700.png "title=" Settimeout.png "alt=" Wkiol1z6d9-qrbqsaaba2wjuz8y700.png "/>
This will wait for 1000ms to complete each execution before the next task is loaded. The first time you write a blog, what's the problem? Welcome to Discussion
This article is from the "sub-Mo Shou" blog, please be sure to keep this source http://zimoushou.blog.51cto.com/4306790/1727652
Timer problems and solutions in JavaScript