price quote

Register/ Signup

Login

Essay Samples

Essay Topics

How to Write Guide

How to Write An Essay

Essay Help

Research Papers

Research Paper Topics

Book Review

Blog

JavaScript Tutorial

why choose us

email us

Agent Signup
Free PR Checker
home buttonour services buttonplace order buttonwhy choose us buttoncontact us button
place order to get custom essay, custom term paper, custom research paper, thesis, disserationsign up to receive free and custom essays, term paper, research paper, thesispromise to provide quality essays, term paper, research paper, thesisearn money affiliate agent programfree sample essays, term paper, research paper
Our Prices

Flash Order: (8 Hours)

$39/page Order Now

Rush Order: (2 Days)

$29/page Order Now

Normal Order: (5 Days)

$25/page Order Now

Economic Order: (14 Days)

$20/page Order Now

Super Saver: (2 Months)

$15/page Order Now

Order Now
Limited Time Discount
Our Services
Essay Writing Services
Essay Help
Custom Essay & Term Paper Writing
Research Dissertation
Admission Documents
Editing & Proof Reading
Computer Applications
Web Design
How to Write Essays
How to an Write an Essay
How to Write a Book Report
How to Write a Thesis
Free Essay Samples
Abortion
Book Review: Get to Work
Film Review-Sicko
Sigmund Freud
Essays Topics
Application Essay
Argumentative Essay
Love Essay
Persuasive Essay
Buying Custom Essays
Buy Essay
Buy a Term Paper
Buy a Research Paper
Buy a custom essay

Timing Events

  • Execute some code NOT immediately after a function is called, but after a specified time interval.
  • 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()
    • Both are methods of the HTML DOM Window object.

setTimeout()

Syntax

var t=setTimeout("javascript statement",milliseconds);
  • setTimeout() returns a value stored in variable t. If you want to cancel this setTimeout(), you can refer to it using the variable name.
  • 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.
  • There are 1000 milliseconds in one second.

Alert Box displayed 5 seconds after button clicked:

<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>

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 button is clicked, the input field will start to count (for ever), starting at 0:

<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);
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!"
onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>
</html>

clearTimeout()

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!" button that stops the timer:

<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>