Dorokhov.codes

12. setTimeout & setInterval

setTimeout

setTimeout sets a timer which executes a function once the timer expires.

var timeUp = function () {
    alert("Time's up!");
};

var timeoutID = setTimeout(timeUp, 3000);

This function returns timeoutID - positive integer value which identifies the timer.

clearTimeout() cancels a timeout.

clearTimeout(timeoutID);

setInterval

setInterval() repeatedly calls a function with a fixed time delay between each call.

var playSound = function() {
    console.log("Ding-dong");
}

var intervalId = setInterval(playSound, 1000);

clearInterval() cancels the action.

clearInterval(intervalId)