Output
The above code will execute in two sections. Firstly, the HTML part of the code will execute, where by
licking on Click Here button the remaining JavaScript code will execute after 3 seconds. See the
output below:
[ADVERTISEMENT
hips:shwwjavatpoin comjavaseriptsimer am‘1624, 2:14 PM Javascrit timer javatpoint
Example of delay the execution of function
[Giekriere]
On clicking the Click Here button, the remaining code will execute after 3 seconds. A message
Welcome to javaTpoint will display on the web after 3 seconds (3000 milliseconds).
Welcome to JavaTpoint
setinterval()
The setinterval method is a bit similar to the setTimeout( function. It executes the specified function
repeatedly after a time interval. Or we can simply say that a function is executed repeatedly after a
specific amount of time provided by the user in this function, For example - Display updated time in
every five seconds.
The basic syntax of setinterval() is:
setInterval(function, milliseconds)
Similar to setTimeout) method, it also accepts two parameters in which one is a user-defined
function, and another is a time-interval parameter to wait before executing the function. The time-
interval parameter holds the amount of time in milliseconds (1 second = 1000 milliseconds), which is
optional to pass. Now, see the code below how this function works:
Execution of code at a regular interval
hnips:ihwwijavatpoin comavaseriptsimer am24, 2:14 PM JavaScript imer-Jvaipint
Updated time will show in every 4 seconds
The current time on your computer is:
Output
On executing the above code, the response will be a simple HTML statement without any time string
like below output:
Updated time will show in every 4 seconds
The current time on your computer is:
After 4 seconds, the JavaScript function will call and start displaying the time. This will repeatedly
display your system time in every four seconds,
Updated time will show in every 4 seconds
The current time on your computer is:
6:19:11 PM
Example ®
One more example of setinterval) methods for displ
continuously.