English 中文(简体)
咖啡厅
原标题:Coffeescript setInterval in class

上周,我开始撰写咖啡稿,因为我正在规划一个新的“游戏20”场,咖啡版是这个标准。 我想每5分钟更新一次我班子的自动数据交换功能,但固定的间隔功能对我的班子没有约束力。 只是在第一次打电话给Data,因为这个目标仍然可以达到,因为固定的Interval(间歇)功能是从建筑商内部要求的。

但是,在发出第一次呼吁之后,这套规定是 间隔期与Widgete案件没有任何关系,也不了解这一作用是什么(以及如何达到)。

Does someone know how to do it?

我的守则是:

class Widget
  constructor: (@name) ->
    this.setUpdateInterval()

  getData: ->
    console.log "get Data by Ajax"

  setUpdateInterval: (widget) ->
    setInterval( this.getData(), 3000000 )
最佳回答
问题回答

问题是,你执行这一职能,而不是提及它。

现在,正如你一样,它也有必要保持这一榜样的范围。 <代码>do和=>可予以帮助。

 setUpdateInterval: (widget) ->
    setInterval (do =>
      @getData), 3000000
    true

汇编

Widget.prototype.setUpdateInterval = function(widget) {
      var _this = this;
      setInterval((function() {
        return _this.getData;
      })(), 3000000);
      return true;
    };

您将注意到该守则执行自动援引功能,该功能将恢复功能,从而在“条码”栏目上设置了封闭功能(如“条码”_该条/条码”)。

还指出,你不需要通过这种方法(在任何地方都使用这种方法),你将在你的建筑商中援引这一功能,以建立间隔。 不管怎样,你只想一提这种方法。 你们只能把这一职能的内容放在你的建筑中。

最后,由于咖啡稿从所有职能中收回了最后一份声明的价值,我在此扔下了一个<>true,但可能没有必要。

这也是一纸空文。 它是Tass的回答。

class Widget
  constructor: (@options = {}) ->
    @options.interval ?= 1000
    @setInterval()

  timer: ->
    console.log  do something 

  setInterval: ->
    cb = @timer.bind @
    setInterval cb, @options.interval

w = new Widget()




相关问题
Fade HTML element with raw javascript

It s my second question of the day related to the same problem, so I apologize for that. I was able to put together a function to "fade out" an element, and it works just fine, my problem is that ...

JavaScript setTimeout setInterval within one function

I think I might be overtired but I cannot for the life of me make sense of this, and I think it s due to a lack of knowledge of javascript var itv=function(){ return setInterval(function(){ sys....

setInterval alternative

In my app I am polling the webserver for messages every second and displaying them in the frontend. I use setInterval to achieve this. However as long as the user stays on that page the client keeps ...

JavaScript setInterval loop not holding variable

Here is my code: var showNo = 1; window.setInterval(function() { console.log(showNo); if(showNo === 1) { var nextNo = 2; } else if(showNo === 2) { var ...

JavaScript Mac Firefox setInterval() Wierdness

I first encountered a problem with safari, where set interval would behave unpredicatbly when the function name was not enclosed within quotations (and optionally it seams with added parentheses): ...

Weird random value as default interval handler parameter

Just came across this. It s not affecting anything really but i m wondering why it s happening. If I run the following code in firefox with firebug on: setInterval(function(param) { ...

simulating a synchronous XmlHttpRequest

I ve read some of the other related questions (Pattern for wrapping an Asynchronous JavaScript function to make it synchronous & Make async event synchronous in JavaScript & there may be more),...

How to exit from setInterval

I need to exit from a running interval if the conditions are correct: var refreshId = setInterval(function() { var properID = CheckReload(); if (properID > 0) { <--- ...

热门标签