5,548,129 members and growing! (18,295 online)
Email Password   helpLost your password?
Web Development » ASP » General     Intermediate

Demonstration of using a timer on a webpage

By Gaurav Saini

Demonstration of using a timer on a webpage.
VBScript, HTMLWindows, NT4, Win2K, WinXP, ASP, Visual Studio, IE 6.0, IE 5.5, IE, Dev

Posted: 22 Nov 2002
Updated: 12 Sep 2003
Views: 83,967
Bookmarked: 27 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 2.86 Rating: 2.05 out of 5
4 votes, 25.0%
1
3 votes, 18.8%
2
0 votes, 0.0%
3
3 votes, 18.8%
4
6 votes, 37.5%
5

Introduction

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 email@gauravsonline.com if you have any queries related to the subject. I would definitely reply.

Example

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 
        EndTime 
        '**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 
        sec=sec-1 
        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 
        min=min-1 
        sec=59 
        status=hr & ":" & min & ":" & sec 
        intTimerID=setTimeOut("HandleTime",950, "VBScript") 
    elseif hr>=0 and min=0 then 
        hr=hr-1 
        min=59 
        sec=59 
        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.

Warning

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

Screenshots of the Program

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:

License

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


The author has Master's degree in Computer Applications (MCA-2004) from IMS, Dehradun (Uttarnchal). Presently working with Cognizant Technology Solutions, Pune. He has experience in J2EE/J2ME and PERL/CGI programming.

He can be reached at (+91) 9923697395.

Occupation: Web Developer
Location: India India

Other popular ASP articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Subject  Author Date 
Generalsending data automatically when the time runs outmembermhelo23:48 1 Apr '08  
QuestionTimer Controlmemberbhappi22:22 23 Jan '08  
Questiontimer codingmemberbhappi22:18 23 Jan '08  
GeneralActiveX that displays images using ASPmemberawni9:32 12 Jan '04  
GeneralFails to meet expectationsmemberPFunky13:49 15 Sep '03  
GeneralRe: Fails to meet expectationsmemberGaurav Saini5:01 16 Sep '03  
Generalhow can i run it in the server ??memberyanivbd3:09 5 Sep '03  
GeneralEhmemberVictor Vogelpoel6:41 23 Nov '02  
GeneralRe: Ehmembergauravsaini3:21 25 Nov '02  
GeneralRe: EhmemberVictor Vogelpoel5:46 25 Nov '02  
GeneralRe: EhmemberSteve McLenithan6:43 25 Nov '02  
GeneralRe: Ehmembergauravsaini19:25 26 Nov '02  
GeneralRe: EhmemberVictor Vogelpoel22:13 26 Nov '02  
GeneralRe: EhsussAnonymous14:56 24 Jun '03  
GeneralRe: EhsussAnonymous20:05 24 Jun '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Sep 2003
Editor: Smitha Vijayan
Copyright 2002 by Gaurav Saini
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project