window.setInterval() or setInterval()
setInterval() executes the code repeatedly at specific time intervals until the interval is canceled or the page is unloaded.
The setInterval() method accepts:
- the code to execute
- the milliseconds to wait between executions.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
setInterval(function() {
document.writeln("Hello world!");
}, 1000);
</script>
</body>
</html>
The setInterval() method returns an interval ID. We can use the ID to cancel the interval with clearInterval() method.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<script type="text/javascript">
var num = 0;
var max = 10;
var intervalId = null;
function incrementNumber() {
document.writeln(num++);
//if the max has been reached, cancel all pending executions
if (num == max) {
clearInterval(intervalId);
document.writeln("Done");
}
}
intervalId = setInterval(incrementNumber, 500);
</script>
</body>
</html>
Home
JavaScript Book
DOM
JavaScript Book
DOM
Window:
- The Window Object
- The Window Events
- window.alert() or alert()
- window.close()
- window.confirm() or confirm()
- window.find() displays find dialog
- window's outer width and height, inner height and width
- window.location
- window.moveBy(distance, distance)
- window.moveTo(x,y) moves the window to the upper-left coordinate
- window.open()
- window.print()
- window.prompt() and prompt()
- window.resizeTo(x,y) and window.resizeBy(xDelta,yDelta)
- window.scrollTo(x,y)
- window.screenLeft, window.screenX, window.screenTop, window.screenY
- window.setInterval() or setInterval()
- window.setTimeout() or setTimeout()
- Pop-up Blockers