这篇文章主要介绍了javascript实现的stopwatch功能,结合具体实例形式分析了javascript自定义stopwatch实现测试运行时间功能的相关操作技巧,需要的朋友可以参考下
本文实例讲述了Javascript实现的StopWatch功能。分享给大家供大家参考,具体如下:
有时会需要js来写一些函数进行测试,如果需要测试执行时间,可能需要一个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);
};用法示例(测试斐波那契数列):
立即学习“Java免费学习笔记(深入)”;
<!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中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号