Examples to explain the use of settimeout () in JS _javascript skills

Source: Internet
Author: User
Tags define function

This article explained the use of JS in settimeout (), share for everyone to reference, the specific content as follows

Effect Chart:

Specific code:

 
 

Here's a specific usage:

1. Parameter
code (required): The JavaScript code string to execute after the function to invoke.
Millisec (required): The number of milliseconds to wait before executing code.
Tip:
settimeout () executes only one code at a time. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.
2. Example

 
 

3, examples (2)

Functionclockon (bgclock) {
varnow=newdate ();
Varyear=now.getfullyear ();
Varmonth=now.getmonth ();
Vardate=now.getdate ();
Varday=now.getday ();
Varhour=now.gethours ();
Varminu=now.getminutes ();
Varsec=now.getseconds ();
Varweek;
 
month=month+1;
if (month<10) month= "0" +month;
if (date<10) date= "0" +date;
if (hour<10) hour= "0" +hour;
if (minu<10) minu= "0" +minu;
if (sec<10) sec= "0" +SEC;
/*vararr_week=newarray ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
Week=arr_week[day];
*
switch (day) {
case1:week= "Monday";
case2:week= "Tuesday";
case3:week= "Wednesday";
case4:week= "Thursday";
case5:week= "Friday";
case6:week= "Saturday";
default:week= "Sunday";
Vartime= "";
time=year+ "Year" +month+ "month" +date+ "Day" "+week+" "+hour+": "+minu+": "+SEC;
if (document.all) {
bgclock.innerhtml= "system Bulletin: [" +time+ "]"
}
vartimer=
settimeout ("Clockon" ( Bgclock);
}

4. Implementation

We often encounter a situation where code in settimeout (code,millisec) contains formal parameters,
For example, we need to store the user-prompted information in variable msg in 1 seconds.

var msg= ' 1shaspassed! ';

At this point, whether directly executed

settimeout (
alert (msg), 1000);//alert (msg) will be executed immediately

Still is

SetTimeout ("Alert (msg)", 1000)//System error msgisnotdefined (Chrome)

Can not achieve the intended purpose, because the timer will try to convert code into a function object, the first error in the case of the timer immediately execute code to return a function object, the result is empty; the second, while successfully encapsulating a function object, But the timer executes code in the visible domain of MSG, so MSG is not delivered correctly
The recommended workaround is to use the anonymous function callback

var msg= ' 1shaspassed! ';
settimeout (function () {
alert (msg);
},1000);

The first parameter passes a function object that invokes the desired statement, which solves the problem of the code band parameter.

The above is the entire content of this article, I hope to learn JavaScript program to help you.

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.