JS Event Delay Execution description Analysis _javascript skills

Source: Internet
Author: User
Tags setinterval

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.