The setInterval() method

The setInterval() method is used to execute a JavaScript function repeatedly at a set interval.

window.setInterval("function()",time);

If you want to pop up an alert every 5 seconds then you can use the following code:

<body>

<p>An alert will pop up every 5 seconds</p>

<script type="text/javascript" >
function AnnoyingOrange() {
	window.alert("I am an annoying orange!");
}
window.setInterval("AnnoyingOrange()",5000);
</script>

</body>

In this case the function is called inside quotes. This prevents the function from executing immediately rather than at the set interval. The function name is sent as a string parameter to the setInterval() method where it is then called at the correct time-lapse.

Leave a Reply