Javascript Timing

Timing

JavaScript code can be executed in time-intervals either by set a timeout and then execute a funciton or set an interval and then execute a function repeatedly.

setTimeout(function, milliseconds)

// wait 3 seconds and then execute function
let timeout = setTimeout(function(){
    console.log("timeout!");
}, 3000);

clearTimeout(setTimeout)

/**
 * clearTimeout
 * The clearTimeout() method stops the execution of the function specified in setTimeout().
 */

// stop timeout from executing.
clearTimeout(timeout);

setInterval(function, milliseconds)

/**
 * setInterval(function, milliseconds)
 * The setInterval() method repeats a given function at every given time-interval.
 * 
 */

let interval = setInterval(() => {
    console.log("fire interval!");
}, 1000);

 

clearInterval(setInterval)

/**
 * 
 * clearInterval(interval);
 */

setTimeout(() => {
    // stom interval after 6 seconds
    clearInterval(interval);
}, 6000);

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *