SetTimeout and SetInterval instance codes and differences

Source: Internet
Author: User
Tags setinterval time interval

Document with its own method:
Loop execution: var Timeid = Window.setinterval ("Method name or method", "delay"); Window.clearinterval (Timeid);
Timed execution: var tmid = Window.settimeout ("Method name or method", "delay"); Window.cleartimeout (Tmid);
An example is provided:

<script type= "Text/javascript Tutorial" >
Loop execution, execute once every 3 seconds Showalert ()
Window.setinterval (function () {
Showalert ("AAAAA");
}, 3000);
function Showalert (mess)
{
alert (mess);
}
Timed execution, 5 seconds to execute Showalert ()
Window.settimeout (function () {
Showalert ("bbbbbb");
},5000);
</script>

<script type= "Text/javascript" >
Loop execution, execute once every 3 seconds Showalert ()
Window.setinterval (Showalert, 3000);
function Showalert ()
{
Alert ("AAAAA");
}
Scheduled execution, 5 seconds to execute show ()
Window.settimeout (show,5000);
Function Show ()
{
Alert ("BBB");
}
</script>

SetTimeout and setinteval Their syntax is essentially the same, but the complete function is different.

The SetTimeout method is the timer program, which is what to do after the time. Just finish it.

The SetInterval method is to indicate that an operation is repeated at a certain time interval.

If you implement the function of Setinerval with settimeout, you need to call yourself in the program you are executing. If you want to clear the counter you need to invoke different cleanup methods depending on the method you are using:

For example: Tttt=settimeout (' Northsnow () ', 1000);

Cleartimeout (TTTT);

Or:

Tttt=setinterval (' Northsnow () ', 1000);

Clearinteval (TTTT);

Give an example:

<div id= "Liujincai" ></div>
<input type= "button" name= "Start" value= "Start" onclick= ' Startshow ' (); ' >
<input type= "button" name= "Stop" value= "Stop" onclick= "Stop ();" >
<script language= "JavaScript" >
var intvalue=1;
var timer2=null;
function Startshow ()
{
liujincai.innerhtml=liujincai.innerhtml + "&nbsp;" + (Intvalue + +). toString ();
Timer2=window.settimeout ("Startshow ()", 2000);
}
function Stop ()
{
Window.cleartimeout (TIMER2);
}
</script>

Or:

<div id= "Liujincai" ></div>
<input type= "button" name= "Start" value= "Start" onclick= " Timer2=window.setinterval ("Startshow ()");//startshow (); ' >
<input type= "button" name= "Stop" value= "Stop" onclick= "Stop ();" >
<script language= "JavaScript" >
   var intvalue=1;
   var timer2=null;
   function startshow ()
   {
      Liujincai.innerhtml=liuj incai.innerhtml + "&nbsp;" + (Intvalue + +). toString ();
  }
   function Stop ()
   {
      window.clearinterval (timer2);
  }
</script>

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.