JavaScript Timer settimeout Refresh window and close window (code super Simple) _javascript tips

Source: Internet
Author: User

No more nonsense to say, directly to everyone paste code.

Refresh the current window every five seconds
settimeout ("Self.location.reload ();", 5000);
JS timed to close the window (IE and FF tested)
//6 seconds after automatically close the current window
settimeout ("Window.opener=null;window.close ()", 6000);

Below to introduce the JavaScript timer use

Using the timer to implement JavaScript's deferred execution or repeated execution of the Window object provides two ways to implement the timer effect, respectively

Window.settimeout () and Window.setinterval. The former can cause a piece of code to run after a specified amount of time, while the latter can cause a piece of code to run once per specified time. Their prototypes are as follows: Window.settimeout (expression,milliseconds); Window.setinterval (Expression,milliseconds); Where expression can be either a string or a function name. is a string can be with parameters, function name can not take parameters, if the parameters with the direct execution of functions, will not delay.

 function Hello () { 
console.log (' I Am Dada ');//alert (' I am ' + name)
; SetTimeout (arguments.callee,2000); 
} SetTimeout (hello,5000);//5 seconds after execution of settimeout (' Hello () ', 3000);//3 seconds to execute

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

The second case is the string, the JS code can be executed, with parameters, but the performance than the function name difference

The third is to call the function and execute it directly

So if you want to wear parameters, but don't want to be called by string, you can write a method yourself:

function _hello (_name) {return 
function () { 
Hello2 (_name); 
} 
} 
SetTimeout (_hello (name), 7000);/immediately executed

First, settimeout

settimeout (function () {
//code to execute 
},200);

After 200ms, the timer code is added to the queue, waiting for the JavaScript process to be idle before the code executes

Second, setinterval

1. The above code refers to the creation of a timer to execute code every 200ms.
2. When using SetInterval, the timer code is added to the queue only when there is no other code instance of the timer (in the queue), referencing the statement in the second edition of JavaScript Advanced programming (that is, when the current timer code executes, The first timer code that follows is added to the queue, waits for execution, and the following timer code is not added to the queue.

When you use setinterval to perform repetitive behaviors, you encounter a problem:

When the timer code executes (if it takes 600ms to finish), it exceeds the specified interval (this is 200ms). Then some timer code will be skipped (that is, the following timer code will not be added to the queue), after the execution of the previous timer code, the queue of the timer code immediately executed, The code execution between the timers is not spaced. At this point, you need to use chain settimeout.

The benefits of doing this are: the previous timer to execute the code execution and wait for 200ms, before creating a new timer, and add the timer code to the queue execution that is: The timer code is not skipped situation; The code execution between timers can be done in intervals (depending on your own settings).

settimeout (function () {
//code to execute 
settimeout (arguments.callee,2000); 
},2000);
SetInterval (function () {
//code to execute 
},200);

This article is to introduce so many people, I hope to help you, at the same time, thank you for your support for cloud-dwelling community website.

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.