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]