Questions about starting the timer and canceling the timer

Source: Internet
Author: User

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

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.