| JavaScript Timers | | Print | |
One very powerful aspect of JavaScript is the ability to set a delay before performing an action. For example, if a user is taking an online quiz that requires him to finish within a given amount of time, you can use a JavaScript timer to warn him when he was nearing the end of his allotted time.
The following very simple example demonstrates the use of two independent timers:
<html>
<head>
<title>
JavaScript Professional Projects - Timers
</title>
<script language="JavaScript">
<!--
var timeout1, timeout2;
function onLoad()
{
var command = "alert( 'You have only 10 seconds left!' );";
timeout1 = window.setTimeout( command, 20000 );
timeout2 = window.setTimeout( "timesUp();", 30000 );
}
function timesUp()
{
alert( "Time is up!" );
document.theForm.theButton.disabled = true;
}
function checkAnswer()
{
window.clearTimeout( timeout1 );
window.clearTimeout( timeout2 );
if( document.theForm.theAnswer.value == "3x2+10x" )
{
alert( "That is correct!" );
}
else
alert( "Sorry, the correct answer is: 3x2+10x" );
}
}
// -->
</script>
</head>
<body onLoad="JavaScript: onLoad();">
<center>
<font size=6>JavaScript Professional Projects</font><br>
<font size=4>Chapter 8: Timers</font>
</center>
<br><br>
<br>
You have 30 seconds to answer the following question: <br>
<br>
<form name="theForm" onSubmit="JavaScript: checkAnswer();">
What is the differential of the following function:
<font face="Courier">x<sup>3</sup>+5x<sup>2</sup>+</font>9 ?
<input type="text" name="theAnswer" size="20"><br>
<br>
<input type="submit" value="Submit" name="theButton">
</form>
</body>
</html>
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Tags: javascript, java script, html, free code, window, menu, form, popup, source, date calculator, calendar, javascripts, timers, javascript help, javascript popup window, forms, calendars, games, Web design builder, webmaster, development.
| Users' Comments (0) |
|
No comment posted






