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.
Ø setTimeout()
Syntax : setTimemout(“javascript statement”,millisecond)
Example
<html>
<head>
<script type="text/javascript">
setTimeout("alert('Hello World')",2000)
</script>
</head>
</html>
Ø clearTimeout()
Syntax : clearTimemout(settimeout variable)
Example
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
function timedCount()
{
document.getElementById('txt').value=c;
c=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>
Posted By : Hemangi Zala
No comments:
Post a Comment