JavaScript Timer SetTimeout Timer refresh window and close window of code is super simple

  • 2021-01-14 05:38:01
  • OfStack

Nonsense not to say much, directly to everyone posted the code.


//  every 5 Refresh the current window every second 
setTimeout("self.location.reload();",5000);
//js  Close the window regularly (ie and FF In the tested )
//6 Automatically closes the current window after seconds 
setTimeout("window.opener=null;window.close()",6000);

Let's introduce the use of javascript timer

The window object provides two methods to implement the effect of a timer, respectively

window. setTimeout () and window setInterval. The former can make a piece of code run after a specified time; The latter can cause a piece of code to run once every 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. A string can take an argument, a function name cannot take an argument. If it takes an argument, the function executes directly without delay.


 function hello (){ 
console.log('I am dada'); //alert('I am ' + name);
//setTimeout(arguments.callee,2000); 
} setTimeout(hello,5000);//5 Seconds after implementation  setTimeout('hello()',3000);//3 Seconds after implementation 
setTimeout(hello(),8000);// immediately  

The first case is a function name that takes no arguments

The second case is a string, an executable js code that can take arguments, but is less performing than a function name

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

So if you want to pass an argument, but don't want to call it as a string, you can write a method of your own:


function _hello(_name){ 
return function(){ 
hello2(_name); 
} 
} 
setTimeout(_hello(name),7000);// immediately 

1. setTimeout


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

The timer code is added to the queue after 200ms and is not executed until the JavaScript process is idle

2. setInterval

1, The above code refers to every 200ms to create a code execution timer
2, when using setInterval, only when the timer () in the queue without any other code examples, to the timer code added to the queue, reference JavaScript version 2 high-level programming statements in the book (i.e., the current timer 1 code executes, the following code will be added to the first timer queue, waiting for execution, to the back of the timer code won't be added to the queue)

There is a problem when using setInterval to perform repeated actions:

When the timer code execution time (if need ms just after 600) more than a specified interval of 200 ms (here), so some timer code will be skipped (that is, the back of the timer code) will not be added to the queue, 1 after the timer code execution, before the timer queue code executed immediately, of all the code execution without the timer interval. At this point, you need to use chained setTimeout.

The advantage of this is: the code to be executed by the first timer is finished and wait for 200ms before creating a new timer, and the timer code is added to the queue for execution, that is: there will be no skipping of the timer code; Code execution between timers can be spaced (depending on your own setting).


setTimeout(function(){
// The code to execute  
setTimeout(arguments.callee,2000); 
},2000);
setInterval(function(){
// The code to execute  
},200);

Related articles: