Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Using setTimeout/setInterval

The setTimeout method is a method of the window object it's purpose is to allow a thread to be created. A thread is a process within a whole process. For example you could show an animated message on the status bar and have the background color of the document alternate two colors. The setTimeout method can also be used to have something happen after a delay (A specified period of time). For example show an alert box every five seconds, however I wouldn't suggest doing that because it would realy get on somebody's nerves.

The following illustration will keep informing the user that the web site is going to move in two days. They will keep being informed until they click the "do not remind me again" button displayed with the message on a prompt. Run Code | View Code

Process

Events

Input

Event Trigger

The onLoad event of the document will call showReminder. showReminder will call it'self until the user clicks the confirm button.

The Solution

Define the body element

Make the onload event of the document call the showReminder function <body bgcolor="#FFFFFF" onLoad="showReminder()">

Define showCurrentTime

Function analyses

The global timerId variable is declared outside of this function so a reference can be kept alive.

showReminder's purpose is to keep reminding the user that the web site will move in two days time.

This function first declares doNotRemind local variable. This variable is used to hold the answer returned by the prompt method.

Then it uses the prompt method of the window object to get the user's answer to the question used with prompt - This web site is moving to http://JShoutcut.com in two days! do not remind me again!" If the user clicks the confirm button true is returned otherwise false or null is. The value is inserted into doNotRemind.

The doNotRemind variable is checked if it is true

If it is true the timmerId variable is accessed and cleared by setting the clearTimeout method on it. This method forces the script engine to free the memory taken by the timerId and this function is never called again. (Unless the user reloads the document)

If doNotRemind is false it means the user wants to be reminded again so setTimeout is used to call this function again after two seconds. When using setTimeout it returns a pointer and is set into timerId.

Comments with code

function showReminder(){
Declare the local variable doNotRemind var doNotRemind Use the prompt method of the window object to inform the user and to ask if they Do Not want to be reminded again. If the user activates the confirm button true is returned otherwise false is doNotRemind = confirm("This web site is moving to http://JShoutcut.com in two days!\do not remind me again!") if the user does not want to be remained again if(doNotRemind){
Use the clearTimeout method of the window object to free the memory and never call this function again (unless the user reloads the document) so the user will never be informed again. clearTimeout(timerId)
The user wants to be remained again }else{
Use the setTimeout method since I do not want to call this function straight away but rather wait for a two second delay then call it. Give the setTimeout method this function's name as the first parameter (in quotes) and the delay as the second parameter (in no quotes) This method returns a pointer to this setTimeout that is cleared as soon as doNotRemind is true by using clearTimeout method. timerId = setTimeout("showReminder()",2000)
}
}

Importent

Run Code | View Code

See also: Getting the time | Using prompt


Using alert | Using the status bar | Changing the background color of the document | Changing the content of a document | Using prompt | Using confirm | Changing the URL of a window Manually | Querying a text field's value | Checking the selected value of a select object | Checking if a checkbox is selected | Checking if a radio button is selected in a group | Making a rollover | Preloading images | Getting the time | Making windows | Making cookies

JavaScript Tutorial Home