Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


JavaScript Timing Events


Reply
Views: 1506  
Thread Tools Rate Thread Display Modes
  #1  
Old 05-03-2009, 06:13 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 81,340
Default JavaScript Timing Events

JavaScript can be executed in time-intervals.

This is called timing events.


JavaScript Timing Events



With JavaScript, it is possible to execute some code after a specified time-interval. This is called timing events.
It's very easy to time events in JavaScript. The two key methods that are used are:
  • setTimeout() - executes a code some time in the future
  • clearTimeout() - cancels the setTimeout()
Note: The setTimeout() and clearTimeout() are both methods of the HTML DOM Window object.


The setTimeout() Method

Syntax

var t=setTimeout("javascript statement",milliseconds); The setTimeout() method returns a value - In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name.
The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like "alert('5 seconds!')" or a call to a function, like "alertMsg()".


The second parameter indicates how many milliseconds from now you want to execute the first parameter.



Note: There are 1000 milliseconds in one second.
Example

When the ****on is clicked in the example below, an alert box will be displayed after 5 seconds.


Example








onClick="timedMsg()" />




Example - Infinite Loop

To get a timer to work in an infinite loop, we must write a function that calls itself. In the example below, when the ****on is clicked, the input field will start to count (for ever), starting at 0:


Example
















The clearTimeout() Method

Syntax

clearTimeout(setTimeout_variable)

Example

The example below is the same as the "Infinite Loop" example above. The only difference is that we have now added a "Stop Count!" ****on that stops the timer:


Example

















Reply With Quote
  #2  
Old 05-03-2009, 06:14 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 81,340
Default

More Examples

Another simple timing











Click on the ****on above. The input field will tell you when two, four, and six seconds have passed.




Reply With Quote
  #3  
Old 05-03-2009, 06:15 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 81,340
Default

A clock created with a timing event









Reply With Quote
Reply

New topics in Java Forum & Tutorial


Thread Tools
Display Modes Rate This Thread
Rate This Thread:



Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
WikiNewForum)