Timer usage for settimeout and setinterval in JavaScript

Source: Internet
Author: User
Tags setinterval

JavaScript's settimeout and setinterval functions are widely used to deal with delays and timed tasks, such as opening a Web page for a period of time after a pop-up login box, the page to send asynchronous requests every time to obtain the latest data and so on. But there are differences in their application.

The settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds, while SetInterval () invokes the function or expression at the specified number of milliseconds, until clearinterval clears it. That is, settimeout () is executed only once, and setinterval () can be executed multiple times. The parameters for two functions are the same, the first is the code or handle to execute, and the second is the number of milliseconds to delay.
SetTimeout usage
The use of the SetTimeout function is as follows:

The

code is as follows:


var timeoutid = Window.settimeout (func, [Delay, param1, param2, ...]);


var timeoutid = window.settimeout (code, [delay]);

Timeoutid: The timer ID number, which can be used to clear the timer in the Cleartimeout () function.
Func: The function that is executed.
Code: (alternative syntax) a coded string to be executed.
Delay: Delay time, in units of milliseconds. If not specified, the default is 0.

We can use Window.settimeout or settimeout, two of them are basically the same, except that the SetTimeout function is referenced as a property of the global Window object.

application Example:

The

code is as follows:


function timeout () {


document.getElementById (' res '). Innerhtml=math.floor (Math.random () *100 + 1);


}


settimeout ("timeout ()", 5000);

When code executes, call the timeout () function after 5 seconds, and click on the demo.
SetInterval usage
The parameters and usages of the setinterval function are the same as the settimeout function, please refer to the use of the settimeout function above. The difference is that setinterval executes the Func or code codes at every interval.
application Example:

The

code is as follows:


var tt = 10;


function Timego () {


tt--;


document.getElementById ("tt"). InnerHTML = TT;


if (tt==0) {


window.location.href= '/';


return false;


}


}


var timer = window.setinterval ("Timego ()", 1000);

The function Timego () defines the content that the page element #tt display, and when TT equals 0 o'clock, the page is directed to the homepage. Then we define a timer timer and use SetInterval () to call Timego () every 1 seconds. This timego will be performed 10 times, each time the number TT will be reduced by 1 until 0. So if you want to stop the timer, you can use the following code:

The

code is as follows:


Window.clearinterval (timer);

Code execution, 10 seconds after the page to jump to the homepage

actually settimeout () can also implement a function repeatedly at intervals, but we still use settimeout and setinterval as a simple distinction. In addition, JavaScript is running in a single-threaded way in the browser's JavaScript engine, the actual application of complex tasks in the need to queue execution, which may lead to timer time is not allowed, this problem in large applications need to consider, this article does not delve into.

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.