LoginSignup
14
10

More than 5 years have passed since last update.

経過時間を順次取得するJavaScript

Last updated at Posted at 2013-11-20
getPast.js
var getPast = (function() {
  var lastDate = NaN;
  return function() {
    var now = Date.now();
    var past = now - lastDate;
    lastDate = now;
    return past;
  };
})();
console.log(getPast());  // 初回はNaN … (1)
// 処理…
console.log(getPast());  // (1)からの経過時間が返る … (2)
// 処理…
console.log(getPast());  // (2)からの経過時間が返る

単純に時間を測りたいだけならこちらも。

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10