当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Javascript实现的StopWatch功能示例

Javascript实现的StopWatch功能示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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);
};

用法示例(测试斐波那契数列):

<!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相关内容感兴趣的读者可查看本站专题:《javascript字符与字符串操作技巧总结》、《javascript数学运算用法总结》、《javascript中json操作技巧总结》、《javascript切换特效与技巧总结》、《javascript查找算法技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript遍历算法与技巧总结

希望本文所述对大家javascript程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网