Ember:每 5 秒轮询一次 api,但在获得上一次调用的响应之后

Ember: Poll an api every 5 secs, but after getting response of the previous call

在转换到路由时,进行 api 调用,等待响应,一旦收到响应,等待 5 秒并再次调用相同的 api 并不断重复此操作,直到我退出了路线。

所以这里的关键是等待 5 秒,在收到响应后,再次执行相同的 api 调用。 我如何在 ember 中实现这一点? 我试过如下轮询,但轮询不能满足我的要求。

 Ember.Route.extend({  
  model: function() {
    var interval = 1000 * 60;
    Ember.run.later(this, function() {
      this.model().then(function(json) {
        this.controller.set('model', json);
      }.bind(this));
    }, interval);

    return Ember.$.getJSON('some api request');
  },

试试这个:

Ember.Route.extend({
  model: function() {
    var interval = 1000 * 60;
    var five = function() {
      this.model().then(function(json) {
        this.controller.set('model', json);
        setTimeout(five, 5000);
      }.bind(this));
    };
    Ember.run.later(this, five, interval);
    return Ember.$.getJSON('some api request');
  },