定时器函数调用2次javascript和enyo

定时器函数调用2次javascript和enyo,javascript,enyo,Javascript,Enyo,})); /* 新Kapping().renderInto(document.body) 其中,beat函数被调用了2次。 另一个是 > var index=0; enyo.kind({ name:"Kapping", events: { onBeat: "" }, create : function(){ this.inherited(arguments); this.timer = window.setInterval(enyo.bind(this,"beat"

})); /*

新Kapping().renderInto(document.body)

其中,
beat
函数被调用了2次。 另一个是

> var index=0;
enyo.kind({
name:"Kapping",
events: {
    onBeat: ""
},
create : function(){
    this.inherited(arguments);
    this.timer = window.setInterval(enyo.bind(this,"beat"),1000);
},
destroy : function(){
    if(this.timer != undefined){
        window.clearInterval(this.timer);
    }
    this.inherited(arguments);
},
beat: function(){
    this.doBeat({
        //this.fetch();
    });
    index = index+1;
    console.log('Doing beat ' + index);
}
在此
中,beat
函数仅被调用一次


我缺少什么?

如果您希望它只运行一次,那么您需要
setTimeout()
而不是
setInterval()

我不知道为什么您会看到不同的行为,但这可能是一个时间问题,因为这两个示例对我来说都是永久运行的。也许代码中的其他地方有问题

> var i =0;
enyo.kind({
name:"Heartbeat",
events: {
    onBeat: ""
},
create : function(){
    this.inherited(arguments);
    this.timer = window.setInterval(enyo.bind(this,"beat"),1000);
},
destroy : function(){
    if(this.timer != undefined){
        window.clearInterval(this.timer);
    }
    this.inherited(arguments);
},
beat: function(){
    this.doBeat({
    });
    i=i+1;
    console.log("In timer callback " + i);
}
});

new Heartbeat().renderInto(document.body);