113、准确的计时器

113、准确的计时器

脚本说明:

第一步:把如下代码加入<body>区域中

<SCRIPT LANGUAGE="JavaScript">

 

<!-- Begin

var ms = 0;

var state = 0;

function startstop() {

if (state == 0) {

state = 1;

then = new Date();

then.setTime(then.getTime() - ms);

} else {

state = 0;

now = new Date();

ms = now.getTime() - then.getTime();

document.stpw.time.value = ms;

   }

}

function swreset() {

state = 0;

ms = 0;

document.stpw.time.value = ms;

}

function display() {

setTimeout("display();", 50);

if (state == 1)  {now = new Date();

ms = now.getTime() - then.getTime();

document.stpw.time.value = ms;

   }

}

// End -->

</SCRIPT>

<FORM NAME="stpw">

本例的时间可以准确到百分之一秒<br>

计时:

<INPUT TYPE="text" Name="time"><br>

<INPUT TYPE="BUTTON" Name="ssbutton" VALUE="开始/停止" onClick="startstop()">

<INPUT TYPE="BUTTON" NAME="reset" VALUE="重置" onClick="swreset()">

</FORM>

 

 

 

第二步:把<body>区域中的内容为

<body bgColor=#fef4d9 onLoad="display()">

 

Write a comment

Comments: 0