如何检测对象是否为间隔/超时?

时间:2016-04-05 19:02:02

标签: javascript node.js

node.js

var timer = setInterval(function(){console.log('hi')},1000);
var notATimer = {};
var number = 2;

detectTimer(timer) //returns true
detectTimer(notATimer) //returns false
detectTimer(number) //returns false

有没有办法可靠地确定对象是否是间隔? 此外,如果检测它的方式也适用于setTimeout,则奖励积分。

4 个答案:

答案 0 :(得分:9)

在nodejs中,setTimeoutsetInterval返回Timeout个实例。该构造函数未导出,但您仍然可以访问它并使用它来检测计时器对象:

const Timeout = setTimeout(function(){}, 0).constructor;
function isTimer(t) { return t instanceof Timeout; }

如果您不想这样做,您还可以通过测试对象具有的属性来使用duck typing。

答案 1 :(得分:8)

setTimeoutsetInterval都会返回一个id引用(在浏览器中)。

在节点中,它们返回对Timeout对象的引用。

所以没有,没有真正的方法可以确定返回的值是来自计时器,还是仅来自浏览器某处的变量。

节点中,您可以从技术上对返回值的构造函数执行instanceof Timeout检查,但我个人不喜欢这样做。< / p>

可以然后将你的计时器实现包装在一些包装对象调用者中,然后你可以检查哪个将在客户端的节点上工作。

例如:

class Timer {
    constructor(callback, time) {
         this.timeId = setTimeout(callback, time);
    }
    clear() {
         clearTimeout(this.timeId);
    }
}

const time = new Timer(() => console.log('hi'), 1000);

console.log(time instanceof Timer); // true

答案 2 :(得分:0)

我认为,正如其他人已经提到的常规方法一样,这是不可能的。但是,您可以创建一个像

这样的包装器
// Timeout, Interval, whatever
class Interval {
    constructor(...args) {
        this.interval = setInterval(...args); // again, whatever fits your needs
        return this;
    }

    /* ... */
}

应用的是

const timer = new Interval(() => console.log('I am an interval'), 1000);

console.log(timer instanceof Interval) // => true (It's an interval)

现在这只是一个基本的例子,但我希望它能给你正确的想法。

答案 3 :(得分:-3)

我建议更改javascript的setTimeout和setInterval函数,我意识到你为node.js标记了这个,所以这里只是一个javascript答案,因为其他答案非常好地处理node.js

window.originalSetTimeout=window.setTimeout;
window.originalClearTimeout=window.clearTimeout;
window.originalSetInterval=window.setInterval;
window.originalClearInterval=window.clearInterval;



window.setTimeout=function(func,delay)
{

    ret=window.originalSetTimeout(func,delay);
    ret +='-timeout';
    return ret;
};
window.setInterval=function(func,delay)
{

    ret=window.originalSetInterval(func,delay);
    ret +='-interval';
    return ret;
};

window.clearTimeout=function(timerID)
{
    tid=timerID.split('-')[0];
    window.originalClearTimeout(tid);
};
window.clearInterval=function(timerID)
{
    tid=timerID.split('-')[0];
    window.originalClearInterval(tid);
};

isTimeout=function(timerID)
{
    t=timerID.split('-')[1];
    if(t == "timeout") return true;
     return false;
}
isInterval=function(timerID)
{
    t=timerID.split('-')[1];
    if(t == "interval") return true;
     return false;
}  

t=setTimeout(function(){console.log('here');},5000);

if(isTimeout(t)) document.getElementById('mess').innerHTML+='<BR>t is a Timeout';
if(isInterval(t)) document.getElementById('mess').innerHTML+='<BR>t is an Interval';

p=setInterval(function(){console.log('Interval here');},5000);
if(isTimeout(p)) document.getElementById('mess').innerHTML+='<BR>p is a Timeout';
if(isInterval(p)) document.getElementById('mess').innerHTML+='<BR>p is an Interval';


  
<div id="mess"></div>