Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
The window object allows execution of code at specified timed intervals. These time intervals are called timing events.
The four key methods to use with JavaScript are:
setTimeout(function, milliseconds)
executes a function, after waiting a specified number of millisecondsclearTimeout()
method clears a timer set with the setTimeout() methodsetInterval(function, milliseconds)
same as setTimeout(), but repeats the execution of the function continuouslyclearInterval()
clears a timer set with the setInterval() method
The window.setInterval
method calls a function or evaluates an expression at specified intervals (in milliseconds). The
setInterval() method will continue calling the function until clearInterval()
is called, or the window is closed.
This example displays the current time (the setInterval() method will execute the function once every 1 second, just
like a digital watch). Once you click the button, it will call the clearInterval
and stop the code.
const ShowMessage = () =>
{
document.getElementById("demo1").innerHTML = "Hello World!";
clearInterval(intervalTimerId);
}
const UpdateDisplay = () =>
{
document.getElementById("demo1").innerHTML = `Showing message in ${countdown}.`;
countdown--;
}
const StopCountdown = () =>
{
console.log("Stopping clock...");
clearInterval(intervalTimerId);
clearTimeout(timeoutTimerId);
};
let countdown = 10;
let delay = 10000;
let timeoutTimerId = setTimeout(ShowMessage, delay);
let intervalTimerId = setInterval(UpdateDisplay, 1000);
http://www.w3schools.com/jsref/met_win_setinterval.asp
https://www.w3schools.com/jsref/met_win_clearinterval.asp
http://www.w3schools.com/js/js_timing.asp