Thinking: When the user across the set variable i=0;
i++ every 100 milliseconds.
It happens to be 1 seconds when i==10. Triggers the event.
Otherwise with clear setinterval I not in + +;
Ha ha. I don't know if I described it clearly.
Code:
Copy Code code as follows:
var delay=function (T,FN) {
var i=0,
J=10,
t= (t*1000)/j,
Divide the delay time evenly into 10 equal portions
_this=this,
Resolves the this binding issue, so when calling the delay function, handle this point to the object itself.
D=setinterval (function () {
i++;
if (i==j) {
Clearinterval (d);
Fn.apply (_this);
};
},T);
_this.onmouseout=function () {
Clearinterval (d);
};
}
Test code:
<div id= "Test" > This is a test div put the mouse over the top 2 seconds before it pops up his id</div> <script type= "Text/javascript" >//<! [cdata[var delay=function (T,FN) {var i=0, j=10, t= (t*1000)/j,//The average latency is divided into 10 equal parts _this=this,//resolve this bind problem, so call the delay function , please deal with this point to itself Object D=setinterval (function () {i++; if (i==j) {clearinterval (d); Fn.apply (_this); }; },T); _this.onmouseout=function () {clearinterval (d); }; } document.getElementById ("Test"). Onmouseover=function () {delay.apply (This,[2,function () {alert (this.id)}]); Use apply to change this pointer}; ]]></script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]