JavaScript timer SetTimeout: regularly refresh and close windows (code is super simple), js timer settimeout

Source: Internet
Author: User

JavaScript timer SetTimeout: regularly refresh and close windows (code is super simple), js timer settimeout

If you don't talk much about it, You can directly post code to everyone.

// Refresh the current window setTimeout ("self. location. reload (); ", 5000); // js closes the window at regular intervals (tested in ie and FF) // closes the current window setTimeout (" window. opener = null; window. close () ", 6000 );

The following describes how to use a javascript timer.

Using the timer to implement JavaScript deferred execution or repeated execution of the window object provides two methods to achieve the timer effect, respectively:

Window. setTimeout () and window. setInterval. The former allows a piece of code to run after a specified time, while the latter allows a piece of code to run once every time. Their prototype is as follows: window. setTimeout (expression, milliseconds); window. setInterval (expression, milliseconds); expression can be a string or function name. A parameter can be included when a string is used. A function name cannot contain a parameter. If a parameter is included, the function is executed directly without delay.

Function hello () {console. log ('I am dada'); // alert ('I am' + name); // setTimeout (arguments. callee, 2000) ;}settimeout (hello, 5000); // after 5 seconds, execute setTimeout ('Hello () ', 3000 ); // execute setTimeout (hello (), 8000) 3 seconds later; // execute immediately

The first case is the function name, but the parameter is not included.

The second case is the string. the executable js Code can contain parameters, but the performance is worse than the function name.

The third is to call a function and directly execute it.

If you want to wear parameters but do not want to call them in the form of strings, you can write a method by yourself:

Function _ hello (_ name) {return function () {hello2 (_ name) ;}} setTimeout (_ hello (name), 7000); // immediately execute

1. setTimeout

SetTimeout (function () {// code to be executed}, 200 );

The timer code is added to the queue after Ms. The code is executed only after the JavaScript process is idle.

Ii. setInterval

1. The above Code refers to creating a timer for code execution every Ms.
2. When setInterval is used, the timer code is added to the queue only when (in the queue) Any other code instance without the timer, reference the statements in the second edition of JavaScript advanced programming (that is, when a timer code is executed, the first timer code that follows will be added to the queue for execution, the subsequent timer code will not be added to the queue)

When setInterval is used to execute repeated behaviors, a problem occurs:

When the timer code execution time (if 200 ms is required) exceeds the specified interval (ms ), some timer code will be skipped (that is, the subsequent timer code will not be added to the queue). After the previous timer code is executed, the timer code in the queue will be executed immediately, there is no interval between the code execution of each timer. In this case, you need to use the setTimeout chain.

The advantage of this is that the code to be executed by the previous timer is executed and wait for Ms before a new timer is created and the timer code is added to the queue for execution: the timer code will not be skipped; the Code Execution between timers can be separated (based on your own settings ).

SetTimeout (function () {// code to be executed setTimeout (arguments. callee, 2000) ;}, 2000); setInterval (function () {// code to be executed}, 200 );

This article will introduce you so much that I hope to help you and thank you for your support for the website.

Articles you may be interested in:
  • Js timer setTimeout cannot call local variable solution
  • SetTimeOut and setInterval timer usage in Javascript
  • Timer usage of SetInterval and setTimeout in javascript
  • Understanding setTimeout and setInterval in javascript timers

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.