1
2
3
4
5
6
7
8
9
10
11
12
|
JavaScript can be executed in time-intervals. This is called timing events. |
With JavaScript, it is possible to execute some code at specified time-intervals. This is called timing events.
It's very easy to time events in JavaScript. The two key methods that are used are:
Note: The setInterval() and setTimeout() are both methods of the HTML DOM Window object.
The setInterval() method will wait a specified number of milliseconds, and then execute a specified function, and it will continue to execute the function, once at every given time-interval.
The first parameter of setInterval() should be a function.
The second parameter indicates the length of the time-intervals between each execution.
Note: There are 1000 milliseconds in one second.
Alert "hello" every 3 seconds:
The example show you how the setInterval() method works, but it is not very likely that you want to alert a message every 3 seconds.
Below is an example that will display the current time. The setInterval() method is used to execute the function once every 1 second, just like a digital watch.
Display the current time:
The clearInterval() method is used to stop further executions of the function specified in the setInterval() method.
To be able to use the clearInterval() method, you must use a global variable when creating the interval method:
Then you will be able to stop the execution by calling the clearInterval() method.
Same example as above, but we have added a "Stop the timer" button:
The setTimeout() method will wait the specified number of milliseconds, and then execute the specified function.
The first parameter of setTimeout() should be a function.
The second parameter indicates how many milliseconds, from now, you want to execute the first parameter.
Wait 3 seconds, then alert "Hello":
The clearTimeoutl() method is used to stop the execution of the function specified in the setTimeout() method.
To be able to use the clearTimeout() method, you must use a global variable when creating the timeout method:
Then, if the function has not allready being executed, you will be able to stop the execution by calling the clearTimeout() method.
Same example as above, but we have added a "Stop the alert" button:
A clock created with a timing event
Start creating your own stunning website. It’s easy and free!
Powerful editing tools and high quality templates make building and launching your website easy and fun! Simply choose a template or start from scratch, change anything, then publish when you’re ready.
Over 20 million users have created their website with Wix.
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
WEB HOSTING |
---|
Best Web Hosting |
PHP MySQL Hosting |
Best Hosting Coupons |
UK Reseller Hosting |
Cloud Hosting |
Top Web Hosting |
$3.98 Unlimited Hosting |
Premium Website Design |
WEB BUILDING |
---|
Download XML Editor |
FREE Website BUILDER |
CREATE HTML Websites |
EASY WEBSITE BUILDER |
W3SCHOOLS EXAMS |
---|
Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP |
W3SCHOOLS BOOKS |
---|
New Books: HTML, CSS JavaScript, and Ajax |
STATISTICS |
---|
Browser Statistics Browser OS Browser Display |
SHARE THIS PAGE |
---|