jiangwei小站
65 字
1 分钟
准时的setTimeout
2024-06-11
function timer() {
  var speed = 500,
    counter = 1,
    start = new Date().getTime();

  function instance() {
    var real = counter * speed,
      ideal = new Date().getTime() - start;

    counter++;

    var diff = ideal - real;
    form.diff.value = diff;

    window.setTimeout(function () {
      instance();
    }, speed - diff); // 通过系统时间进行修复
  }

  window.setTimeout(function () {
    instance();
  }, speed);
}