Home > Web Front-end > JS Tutorial > body text

StopWatch function implemented by Javascript

陈政宽~
Release: 2017-06-28 12:48:33
Original
1283 people have browsed it

This article mainly introduces the StopWatch function implemented by Javascript, and analyzes the related operating skills of javascript custom StopWatch to implement the test running time function based on specific examples. Friends in need can refer to the following

The example in this article describes the StopWatch function implemented by Javascript. Share it with everyone for your reference, the details are as follows:

Sometimes js is needed to write some functions for testing. If you need to test the execution time, you may need a stopwatch:

StopWatch class:

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);
};
Copy after login

Usage example (test Fibonacci sequence):

<!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>
Copy after login

The running effect diagram is as follows:

The above is the detailed content of StopWatch function implemented by Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!