The difference between settimeout and setinterval method

Source: Internet
Author: User

the difference between settimeout and setinterval method

SetTimeout () is used to set the corresponding function or code to execute after the specified time., executed at the global scope

SetTimeout (Code,time[,args ...]) code: function or JS code to be executed, time to wait, args: parameter

such as: function SayHello (name, age) { //popup message after 5 Seconds "My name Codeplayer, 18 years old this year!". 
Alert ("I call" + name + ", this year" + Age + "old!");
   SetTimeout (SayHello, "Codeplayer", 18);
SetInterval () is used to set the corresponding function or code to execute at a specified time , executing at the global scope
SetInterval (Code,time[,args ...]) code: function or JS code to be executed, time to wait, args: parameter
such as: function SayHello (name, age) { //every 5 seconds pops up the message "My name Codeplayer, 18 years old this year!". 
Alert ("I call" + name + ", this year" + Age + "old!");
    SetInterval (SayHello, "Codeplayer", 18);
    The difference between setTimeout () and SetInterval ()
    1. SetTimeOut (), execute once
    2. SetInterval (), executes every few seconds
    SetTimeout () implements the function of SetInterval (), using recursive algorithm
such as: function Test () {
Alert ("Test");
SetTimeout (test,2000);
    }
   SetTimeout (test,5000);
SetInterval () execution within the same time
SetTimeout () The time of first execution and subsequent execution can be different

The difference between settimeout and setinterval method

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.