Internet Explorer 5.5Internet Explorer 6.0IEVBScriptWindows 2000ASPWindows XPHTML中级开发Visual StudioWindows
在网页上演示使用计时器






2.95/5 (16投票s)
2002年11月23日
2分钟阅读

134189

1379
网页计时器演示。
引言
ASP 常用于 Microsoft 服务器和其他兼容平台来构建动态网页,并从数据库中检索数据。ASP 支持各种脚本语言,如 VBScript、JScript 等。这些脚本语言为网页提供业务逻辑,而 ASP 用于为网页提供动态内容。
问题简述
我在为我的学院构建在线考试系统时遇到一个问题。问题是限制学生的时间,即学生必须在特定时间内回答考试中的所有问题。为此,我需要在网页上附加一个计时器。我使用了 ASP 页面中的 VBScript。我假设读者熟悉 VBScript,如果不是,请发送邮件至 email@gauravsonline.com,如果您对该主题有任何疑问。我一定会回复。
示例
现在解释如何完成它。我正在尝试模拟一个“倒计时时钟”。以下是使用以下 ASP 示例进行的说明
'*************************************************************
'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
如何使用源代码
要运行 ASP 代码,您必须在您的系统上安装一个支持 ASP 的 Web 服务器,例如 Microsoft Personal Web Server 或 IIS,并选择发布目录。此代码也可以从一个简单的 .htm 文件中执行,而无需安装 PWS 或 IIS。
当您运行程序时,您会看到时钟从 0:2:0 开始,并结束于 00:00:00。您可以使用程序中的 hr
、min
和 sec
变量来更改时间。此外,您还可以连接一个表单,从中可以指定输入到这些变量,从而可以拥有自己的时间限制。
警告
此代码只能在内置 VBScript 的浏览器中执行。例如:IE 4.0 或更高版本。
程序截图
在这里,您可以看到时间从 0:2:0 开始。时间在两分钟后结束,然后显示如下消息