Click here to Skip to main content
15,075,944 members
Articles / Web Development / HTML
Posted 22 Nov 2002


35 bookmarked

Demonstration of using a timer on a webpage

Rate me:
Please Sign up or sign in to vote.
2.95/5 (16 votes)
12 Sep 20032 min read
Demonstration of using a timer on a webpage.


ASP is commonly used on Microsoft Servers and other compatible platforms to build dynamic web pages and also to retrieve data from databases. ASP supports various scripting languages like VBScript, JScript, etc. These scripting languages provide the business logic to the web pages while ASP is used to provide dynamic content to the webpage.

Briefing About The Problem

I came across a problem when I was building an Online Examination System for my Institute. The problem was to restrict the student's time, i.e., the student must answer all the questions in the exam in a specific time. For that, I had to attach a timer to the web page. I made use of VBScript inside the ASP page. I assume here that the reader is conversant in VBScript, if not, it is not a problem, just mail me at if you have any queries related to the subject. I would definitely reply.


Now comes the explanation of how it's done. I am trying to simulate a "count down clock". This is explained as follows with the help of the following ASP example:

'This program makes use of a recursive procedure HandleTime.
'The important aspects of the code are "Status" and "setTimeOut". 
'Status is an attribute of the Window object in VBScript and is used to 
'change the status message of the active window. 
'setTimeOut is a VBScript function that tells after how much time the timer 
'would stop and accordingly it calls a function passed into it to take some 
'action when the time is up.
'This procedure calls its self after every 1 sec (I have taken 1sec = 950ms
' here) and accordingly the variables hr, min and sec are decremented. 
Sub HandleTime 
    if hr=0 and min=0 and sec=0 then 
        '**When hr, min and sec are all 0, this means that the time is up 
        '**and EndTime procedure is 'called. 
    elseif min>=0 and sec>0 then 
        status=hr & ":" & min & ":" & sec 
        '**Whenever the second changes the Status of the window has to 
        'be changed. 
        intTimerID=setTimeOut("HandleTime",950, "VBScript") 
        '**setTimeOut function returns a unique timer id that is used to 
        'keep track of the time. 
        '**When the time is up the timer id is destroyed automatically. 
    elseif min>0 and sec=0 then 
        status=hr & ":" & min & ":" & sec 
        intTimerID=setTimeOut("HandleTime",950, "VBScript") 
    elseif hr>=0 and min=0 then 
        status=hr & ":" & min & ":" & sec 
        intTimerID=setTimeOut("HandleTime",950, "VBScript") 
    end if 
End Sub 

Sub EndTime 
    clearTimeOut intTimerID 
    '**clearTimeOut is a built in procedure of VBScript which is used to 
    '**clear and destroy the timer id explicitly 
    status = "Your Time Ended" 
    '**When the time ends the status bar displays this message. 
    msgbox "Time Up" 
End Sub

How To Use The Source Code

To run the ASP code, you must have an ASP enabled web server such as Microsoft Personal Web Server or IIS installed on your system and select the publishing directory. This code could also be executed from a simple .htm file without requiring PWS or IIS installed.

When you run the program, you would see that the clock starts from 0:2:0 and ends at 00:00:00. You can use the variables hr, min and sec in the program to change the time. Moreover, you can also connect a form from which you can specify inputs to these variables and hence you can have your own time constraints.


This code could only be executed inside browsers with VBScript built in. For example: IE 4.0 or higher.

Screenshots of the Program

Image 1

Here, you can see that the time starts at 0:2:0. The time ends after two minutes and then the message is displayed like this:

Image 2


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gaurav Saini
Team Leader
India India
Over 10 years of experience in designing & development of Java/J2EE standalone and web based systems for financial & commercial institutions. Over 4 years of experience in enhancing and maintenance of multi-threaded systems. Solid background in Object-Oriented analysis and design, web site development and maintenance. Proficient in developing thorough design documentation of system interfaces, data model and application components. Profound knowledge of design patterns (GOF & J2EE patterns), coding standards and best practices. Deep knowledge in creation of standalone batch based data extraction & loader systems.

Had been to onsite (NY, USA) for 2 years (2009-2011) and worked with technical, implementation team, customer support team and business team for creation, design, implementation, deployment and post deployment support of new web based and batch based systems.

Comments and Discussions

Questiondemonstrate using Pin
swatigodkhindi14-Apr-09 21:54
Memberswatigodkhindi14-Apr-09 21:54 
AnswerRe: demonstrate using Pin
Gaurav Saini15-Apr-09 5:00
MemberGaurav Saini15-Apr-09 5:00 
Generalsending data automatically when the time runs out Pin
mhelo1-Apr-08 22:48
Membermhelo1-Apr-08 22:48 
QuestionTimer Control Pin
bhappi23-Jan-08 21:22
Memberbhappi23-Jan-08 21:22 
Questiontimer coding Pin
bhappi23-Jan-08 21:18
Memberbhappi23-Jan-08 21:18 
GeneralActiveX that displays images using ASP Pin
awni12-Jan-04 8:32
Memberawni12-Jan-04 8:32 
GeneralFails to meet expectations Pin
PFunky15-Sep-03 12:49
MemberPFunky15-Sep-03 12:49 
GeneralRe: Fails to meet expectations Pin
Gaurav Saini16-Sep-03 4:01
MemberGaurav Saini16-Sep-03 4:01 
yes u can do it with either VBScript or JavaScript:
1> disable the menus and the toolbars for ur client
2> create a database that would keep track of the user appearing for the test
3> allow only 2 tries to resume test from backend itself...

u have to be serious for this issue and this can be implemented through programming only...
my article gives only the code of the timer rest the back work ...
hope that solves the problem
Questionhow can i run it in the server ?? Pin
yanivbd5-Sep-03 2:09
Memberyanivbd5-Sep-03 2:09 
GeneralEh Pin
Victor Vogelpoel23-Nov-02 5:41
MemberVictor Vogelpoel23-Nov-02 5:41 
GeneralRe: Eh Pin
Gaurav Saini25-Nov-02 2:21
MemberGaurav Saini25-Nov-02 2:21 
GeneralRe: Eh Pin
Victor Vogelpoel25-Nov-02 4:46
MemberVictor Vogelpoel25-Nov-02 4:46 
GeneralRe: Eh Pin
Steve McLenithan25-Nov-02 5:43
MemberSteve McLenithan25-Nov-02 5:43 
GeneralRe: Eh Pin
Gaurav Saini26-Nov-02 18:25
MemberGaurav Saini26-Nov-02 18:25 
GeneralRe: Eh Pin
Victor Vogelpoel26-Nov-02 21:13
MemberVictor Vogelpoel26-Nov-02 21:13 
GeneralRe: Eh Pin
Anonymous24-Jun-03 13:56
MemberAnonymous24-Jun-03 13:56 
GeneralRe: Eh Pin
Anonymous24-Jun-03 19:05
MemberAnonymous24-Jun-03 19:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.