JavaScript Timing Events

Leave a Comment

With javascript, it is possible to execute some code NOT immediately after a Function is called, but after a specification time interval. this is called timing Events. It is very easy to time events in javascript. The two key methods that are used Are:-
SetTimeout() – executes a code sometime in the future
ClearTimeout() – cancels the SetTimeout()

SetTimeout()

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.

Example:-
<html>
<head>
<script type=”text/javascript”>
Function timedmsg()
{
Var t=setTimeout(“alert(‘5 seconds!’),5000);
}
</script>
</head>
<body>
<form>
<input type=”button” value=”Display timed alertbox!” onclick=”timedMsg()”>
</form>
</body>
</html>   

ClearTimeout()

The cleartimeout() method is used to stop the execution of the Function specification in the settimeout() method.

Syntax:-
ClearTimeout(timeoutvariable)

Example:-
<html>
<head>
<script type =”text/javascript”>
Var c=0;
Var t;
Function timedCount()
{
Document.getElementById(‘txt’).value=c;
cc=c+1;
t=setTimeout(“timedCount()”,1000);
}
Function stopCount()
{
ClearTimeout(t);
}
</script>
</head>
<body>
<form>
<input type=”button” value=start Count!”onclick=”timedCount()”>
<input type=”text”id=”txt”>
<input type=”button” value=”stop Count!” onclick=”stopCount()”>
</form>
</body>
<html>

0 comments:

Post a Comment