setInterval中的当前时间? [英] Current time in setInterval?

查看:119
本文介绍了setInterval中的当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setInterval(function() {
      var current = ? getCurrentInterval ? ;
      alert(current);
}, 2000);

可以检查吗?

推荐答案

没有什么可以给您当前"时间间隔的,因为可能有几个不同的计时器正在运行.您最好构造自己的Timer类,该类存储间隔并可以稍后查询.

There's nothing that will give you the "current" interval, as there might be several distinct timers running. You might be better off constructing your own Timer class that stores the interval and that you can later query.

如果您有多个计时器,并且需要在回调中访问相关的Timer,那么您将不得不在Javascript的作用域上下文中有所创新.遵循以下原则:

You're going to have to be a little creative with Javascript's scoping contexts if you have multiple timers and need to access the relevant Timer inside your callback. Something along these lines:

function Timer(timeout) {
    var self = this;
    this.interval = timeout ? timeout : 1000;   // Default

    this.run = function (runnable) {
        setInterval(function () { runnable(self); }, this.interval);
    };
}

var timer = new Timer(1000);
timer.run(function (timer) {
    alert(timer.interval);
});

这篇关于setInterval中的当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆