» » Usage of setTimeOut and setInterval timer in Javascript

 

Usage of setTimeOut and setInterval timer in Javascript

Author: bamboo06 on 5-11-2015, 05:15, views: 1880

2
SetTimeOut and javascript setInterval function application is very wide, they are used to deal with the delay and timing of tasks, such as web pages for some time after opening up a login box, page every so often to send an asynchronous request to obtain the latest data, and so on. But their application is different.

setTimeout () method is used to evaluate the expression or function call after the specified number of milliseconds, while setInterval () is in the specified number of milliseconds every cycle calling function or expression, clearInterval until it clears. That setTimeout () only once, setInterval () can be executed multiple times. The same parameters of the two functions, the first parameter is the code or handler to be executed, and the second is the number of milliseconds delay.

setTimeOut Usage
Using setTimeout function is as follows:
var timeoutID = window.setTimeout(func, [delay, param1, param2, ...]); 
var timeoutID = window.setTimeout(code, [delay]); 

timeoutID: timer ID, it can clearTimeout () function is used to clear the timer.
func: the function to be executed.
code :( alternative syntax) a code string to be executed.
delay: delay time, in milliseconds. If not specified, the default is 0.
We can use window.setTimeout or setTimeout, both essentially the same wording, but window.setTimeout the setTimeout function as an attribute to the global window object reference.
Application examples:
function timeout(){ 
    document.getElementById('res').innerHTML=Math.floor(Math.random()*100 + 1); 
} 
setTimeout("timeout()",5000); 

When the code is executed, after five seconds call timeout () function, click on to .

setInterval Usage
Parameters and usage setInterval and setTimeout functions functions, refer to setTimeout function described above usage. The difference is, setInterval regular intervals among the func or code execution of code.
Application examples:
var tt = 10; 
function timego(){ 
    tt--; 
    document.getElementById("tt").innerHTML = tt; 
    if(tt==0){  
        window.location.href='/'; 
        return false; 
    } 
} 
var timer = window.setInterval("timego()",1000); 

Function timego () defines the content of the page elements #tt displayed when tt is equal to 0, the page orientation to the home. We then define a timer timer, use setInterval () is called once every 1 second timego (). Such timego performs 10 times digital tt will be reduced by 1 until zero. So if you want to stop the timer, you can use the following code:
window.clearInterval(timer);  

When the code is executed, after 10 seconds the page will jump to the home page, click on to .
In fact setTimeout () can also be implemented from time to time to repeat a function, but we simply use setTimeOut difference and setInterval. In addition to single-threaded manner javascript is running on the browser's javascript engine, the actual application of complex tasks need to line up to perform, which may cause the timer time allowed, the question needs to be considered in large applications, this article does not give in-depth study.

Category: Javascript

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
10 August 2017 21:10

Diamond May

Reply
  • Group: Guests
  • –†Registered date: --
  • Status:
 
I believe that the setTimeOut and setInterval timer in javascript is pretty important. The essayontime review had explained this mere fact to me earlier. And I was impressed by that then, and there. I hope I'm able to use it in my codes.

<
  • 4 Comments
  • 0 Articles
24 December 2017 09:29

JordanDean

Reply
  • Group: Members
  • –†Registered date: 24.12.2017
  • Status: Currently Offline
 
I remember some guy told me about this plugin at https://twitter.com/edussoncom So now I really want to try this plugin. It's interesting for me what I can discover with its help

Information
Comment on the news site is possible only within (days) days from the date of publication.