In the JS mechanism, we all know that the general code is written in an event, only when the event is triggered, the code will be executed, such as
$ (selector). Click (function () {alert (' Hello ')});
As the above code, only when the trigger click event, will pop up "hello" greeting;
However, when the timer is used, both in the event and in the event, it is executed at the time of loading, such as
var timer = setinterval (play,speed); $ (' selector '). Hover (function () { clearinterval (timer);},function () { Timer = setinterval (play,speed); })
The above code, I was in the event to open a timer, when the mouse through the selector, the timer off, the mouse away from the selector, will turn on the timer.
However, the above code will open two timers instead of what we want just to open one, then, if the next makeover
var timer = setinterval (play,speed); $ (' selector '). Hover (function () { clearinterval (timer);},function () { Clearinterval (timer); Timer = setinterval (play,speed); })
Remember, the timer that is written in the event is also opened when it is loaded, so be sure to add a clear timer before the timer is executed.
Questions about starting the timer and canceling the timer