I understand---timers settimeout and cleartimeout

Source: Internet
Author: User

Today in writing a picture switching problem has animation lag problem, only to check settimeout and cleartimeout. The previously written picture player also has a similar problem with the Auto Start button and the Stop button,

Others are normal, the problem is that each time you click the Start button many times, the speed of the picture will be changed a lot, and there is no regularity. I didn't think about it at the time, until today I had a similar problem.

To decide what to do.

Here's a simple summation example:

<! DOCTYPE Html>"Content-type"Content="text/html; Charset=utf-8"><title> Timer </title>"Text/javascript">varnum=0; vari; function Startcount () {document.getElementById ('Count'). value=num; Num=num+1; I=settimeout ("Startcount ()", +);  } function Stopcount () {cleartimeout (i); }</script>"text"Id="Count"/> <input type="Button"Value="Start"onclick="Startcount ()"/> <input type="Button"Value="Stop"onclick="Stopcount ()"/> </form></body>

The effect is as follows:

Click Start to Accumulate. When you click the Start button multiple times, the numbers soar quickly, depending on the speed of your clicks.

function Startcount () {      cleartimeout (i);    document.getElementById (' count '). Value=num;    num=num+1;       I=settimeout ("Startcount ()", +);      }

Later, the Startcount function was added a cleartimeout (i); it solved the problem, and I didn't know why.

Today, I think carefully about the original is so.

Why do multiple clicks of numbers soar when cleartimeout is not set?

1: When we click the Start button, we start to run the function, first show the number 0, and then run to settimeout,1s after the Startcount function, because there is settimeout inside the function, so the function will continue to execute,

And when we click the Start button again, the function executes again, and the function is already executing. Then this function is alternately executed, then the number will be chaotic, the cumulative speed doubled, as to the number of clicks and what is the relationship, no deep research, it is unclear.

2: Why can we avoid this situation after we set up the cleartimeout?

Let's run the function once, click Start, the function starts running, when run to setttimeout set the function 1s and then run again, there is a return value I. After this command is released, let's assume what happens when you click the Start button again within this 1s time? (1s is still a long time, we can click the n-th mouse), the settimeout set to execute the function number A, we click on the trigger to execute the function number B, then B is instantaneous (the speed of the computer is no doubt), and this a has to 0~1s after the execution ( b in a first execution), B executes when the function inside has cleartimeout, so the settimeout set of a canceled, do not have to execute. Then only b in the execution, no matter how the click will not appear chaotic situation.

So the problem comes, you set up the Cleartimeout that doesn't set the settimeout to terminate it? Isn't that going to accumulate?

Honestly, I was wondering, then, to analyze and analyze. Once the function is executed, settimeout sets 1s and then executes the function once, (no function is run without settimeout), the command is executed, we go to execute,

When it comes to the inside of the function (that is, the body of the function), it encounters Cleartimeout. He said you should stop executing the function. But we are already in the execution, and our instructions are to execute this time, we will not execute after the execution is done. The cleartimeout of this time has little effect on our execution function. (because we are only doing this once, there is no next time not to execute the argument), if the cleartimeout placed outside the function body is not the same, I can be outside to intercept you, before you have to execute, have not entered the function of the time to intercept you, so that reached the role of the Stop, Similar to the top button.

END. I understand it myself.

Some of their own understanding, if there is improper, but also hope to pass by the park friend feel free, help me to take the right path.

I understand---timers settimeout and cleartimeout

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.