這篇文章主要介紹了Javascript實現的StopWatch功能,結合具體實例形式分析了javascript自訂StopWatch實作測試運行時間功能的相關操作技巧,需要的朋友可以參考下
本文實例講述了Javascript實現的StopWatch功能。分享給大家供大家參考,如下:
有時會需要js來寫一些函數進行測試,如果需要測試執行時間,可能需要一個stopwatch:
StopWatch類別:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function stopWatch() {
}
stopWatch.prototype.Start = function () {
this.startD = new Date ();
return this;
};
stopWatch.prototype.Stop = function () {
this.startD = new Date ();
return this;
};
stopWatch.prototype.Seconds = function () {
return Math. abs (( new Date () - this.startD) / 1000);
};
|
登入後複製
用法範例(測試斐波那契數列):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "UTF-8" >
<title>StopWatch</title>
</head>
<body>
<script >
function stopWatch() {
}
stopWatch.prototype.Start = function () {
this.startD = new Date ();
return this;
};
stopWatch.prototype.Stop = function () {
this.startD = new Date ();
return this;
};
stopWatch.prototype.Seconds = function () {
return Math. abs (( new Date () - this.startD) / 1000);
};
var sw = new stopWatch().Start();
( function f(n){ return n == 1 || n == 2 ? 1 : f(n-1)+f(n-2);})(45);
alert(sw.Seconds());
</script>
</body>
</html>
|
登入後複製
運行效果圖如下:

以上是Javascript實現的StopWatch功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!