Javascript實現(xiàn)的StopWatch功能示例
本文實例講述了Javascript實現(xiàn)的StopWatch功能。分享給大家供大家參考,具體如下:
有時會需要js來寫一些函數(shù)進(jìn)行測試,如果需要測試執(zhí)行時間,可能需要一個stopwatch:
StopWatch類:
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);
};
用法示例(測試斐波那契數(shù)列):
<!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>
運行效果圖如下:

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript字符與字符串操作技巧總結(jié)》、《JavaScript數(shù)學(xué)運算用法總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript遍歷算法與技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
《JavaScript高級程序設(shè)計》閱讀筆記(三) ECMAScript中的引用類型
ECMAScript中的引用類型,主要包括Object類、Boolean類、Number類、String類、instanceof運算符2012-02-02
JavaScript數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的表示方法示例
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的表示方法,從數(shù)據(jù)結(jié)構(gòu)線性表的角度分析了數(shù)組的原理并結(jié)合實例形式分析了javascript數(shù)組的定義與使用方法,需要的朋友可以參考下2017-04-04

