JS event delay execution description analysis _ javascript skills

Source: Internet
Author: User
The event is triggered to prevent the user from moving the mouse over it unconsciously. A waste of client resources. Idea: when the user sets the variable I = 0;
Every 100 milliseconds I ++
When I = 10, it is just one second. Trigger the event.
Otherwise, it is clear that setInterval I is not in ++;
Haha. I do not know whether the description is clear.
Code:

The Code is as follows:


Var delay = function (t, fn ){
Var I = 0,
J = 10,
T = (Tb * 1000)/j,
// Divide the delay time equally into 10 equal parts
_ This = this,
// Solve this binding problem, so when calling the delay function, Please handle this point to its own object
D = setInterval (function (){
I ++;
If (I = j ){
ClearInterval (d );
Fn. apply (_ this );
};
}, T );
_ This. onmouseout = function (){
ClearInterval (d );
};
}


Test code:

<P id = "test" style = "border: 1px solid # CCC; width: 100px; height: 100px; background: #666; "> This is a test DIV. After you hover your mouse over it for 2 seconds, its ID is displayed. </p> <script type =" text/javascript "> // <! [CDATA [var delay = function (t, fn) {var I = 0, j = 10, t = (t * 1000)/j, // divide the delay time equally into 10 equal portions _ this = this, // solve the this binding problem, so when calling the delay function, please handle this to point to its own 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 select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

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.