The example explains the usage of setTimeout () in JS, and the example explains settimeout.
This article explains the usage of setTimeout () in JS and shares it with you for your reference. The specific content is as follows:
:
Code:
<Html>
The following describes the specific usage:
1. Parameters
Code (required): (the original meaning of the code) the JavaScript code string to be executed after the function to be called.
Millisec (required): The number of milliseconds to wait before executing the code.
Tip:
SetTimeout () is executed only once. If you want to call it multiple times, use setInterval () or let the code itself call setTimeout () again ().
2. Instance
3. instance (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"; break; case2: week = "Tuesday"; break; case3: week = "Wednesday"; break; case4: week = "Thursday"; break; case5: week = "Friday"; break; case6: week = "Saturday"; break; default: week = "Sunday"; break;} vartime = ""; time = year + "year" + month + "month" + date + "day" + week + "" + hour + ":" + minu + ":" + sec; if (document. all) {bgclock. innerHTML = "system announcement: [" + time + "]"} vartimer = setTimeout ("clockon (bgclock)", 200 );}
4. Execute
We often encounter a situation where the code in setTimeout (code, millisec) contains the form parameter,
For example, the information we need to prompt the user one second is stored in the variable msg,
var msg='1shaspassed!';
In this case
SetTimeout (alert (msg), 1000); // alert (msg) will be executed immediately
Or
SetTimeout ("alert (msg)", 1000); // system error msgisnotdefined (chrome)
The timer will try to convert the code into a function object. In the first error example, the timer immediately executes the code to return a function object, and the result is empty; the second one successfully encapsulates the function object, but the timer executes code outside the visible domain of msg, so msg cannot be correctly transmitted.
The recommended solution is to use anonymous function callback.
var msg='1shaspassed!';setTimeout(function(){alert(msg);},1000);
The first parameter passes a function object that will call the required statement, which solves the problem of code with parameters.
The above is all the content of this article, and I hope it will help you learn javascript programming.
Articles you may be interested in:
- Detailed usage of JavaScript window. setTimeout ()
- Javascript setTimeout () transfer function parameters (including passing object parameters)
- When setTimeout () and setInterval () are called and executed in Js
- Usage of setTimeout () in JS
- Introduction and Application of js setTimeout () functions take countdown As An Example
- Notes for using the closure feature in setTimeout () of Javascript
- Analysis of setTimeout () and clearTimeout () usage examples in js