Home  >  Article  >  Web Front-end  >  JS event delayed execution description analysis_javascript skills

JS event delayed execution description analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:471065browse

Idea: When the user crosses, set the variable i=0;
Every 100 milliseconds i
When i==10, it is exactly 1 second. Just trigger the event.
Otherwise, use clear setInterval i is not there;
Haha. I don’t know if I described it clearly.
Code:

Copy code The code is as follows:

var delay=function(t, fn){
var i=0,
j=10,
t=(t*1000)/j,
//Divide the delay time into 10 equal parts
_this=this ,
//Solve this binding problem, so when calling the delay function, please handle this pointing to the own object
d=setInterval(function(){
i;
if(i== j){
clearInterval(d);
fn.apply(_this);
};
},t);
_this.onmouseout=function(){
clearInterval( d);
};
}

Test code:

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn