Search code examples
javascriptember.jspolling

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


On transition to a route, Make an api call, wait for the response and once you get a response, wait 5 secs and again call the same api and keep repeating this, until I exit out of the route.

So the catch here is to wait 5 secs, after getting the response, and again do the same api call. How do i achieve this in ember? I have tried polling as below, but polling doesn't suffice my requirement.

 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');
  },

Solution

  • Try this:

    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');
      },