Home >Web Front-end >JS Tutorial >Delayed repeated execution of functions lLoopRun.js_javascript tips
In one of the company's projects, there are many places where it is necessary to delay the execution of some repeatable functions (actions), so I wrote the following function.
Haha, I don’t know what the more precise meaning is, so it’s: LoopRun, which means "repeated execution"
function lLoopRun(sFuncLoop,sFuncEnd,nDelay) {
var vintervalId = null;
Var RunString = SFunCloop;
VAR Stopstring = SFuncend;
var delaytime = ndlay;
// var nCount = 0;
This._doloop = fuory () {
If (vintervalid && !eval(stopString)){
eval(runString);
//nCount;
} else {
window.clearInterval(vintervalId);
vintervalId = null;
}
}
window.clearInterval(vintervalId);
vintervalId = window.setInterval(this._doLoop,delayTime);
}
Parameter description:
sFuncLoop >> String Type, a Javascript function or statement that needs to be executed repeatedly (please separate multiple functions or statements with ;)
sFuncEnd >> String type, a Javascript function or statement used to terminate repeated execution of actions (sFuncLoop)
nDelay > > Numeric type, time interval of repeated execution (number of milliseconds)
Application example:
Horizontal reciprocating motion: http://cnlei.iecn.net/mycode/lLoopRun/index .html
Auto-scaling size: http://cnlei.iecn.net/mycode/lLoopRun/index2.html
Vertical reciprocating motion: http://cnlei .iecn.net/mycode/lLoopRun/index3.html
Gradient display (picture): http://cnlei.iecn.net/mycode/lLoopRun/index4.html
The above are just a few simple application examples. The key to specific applications depends on whether the functions represented by the two parameters sFuncLoop and sFuncEnd are well written. For example, if the effect of buffering is added to the moving picture in Example 1, , you need to add the corresponding implementation code to the function represented by sFuncLoop:)