0

I'm just wondering why my setTimeout doesn't work in recursive pattern. It ran for just twice and stopped.

Here's the fiddle. https://jsfiddle.net/vp90c10s/

var amqp = function() {
    return {
    connect: function(url, callback) {
        callback();

    }
  }
}

var module = {
    connect: function() {
        var self = this;    
    amqp().connect('', function() {
        console.log('trying to connect');
        setTimeout(self.connect, 1000)
    })
  }
}

module.connect();
toy
  • 11,711
  • 24
  • 93
  • 176
  • 2
    `setTimeout` will call the given function just fine, but not in the context of your `module` object, so it will fail to access `self.connect` the second time. – Bergi Feb 12 '16 at 15:01
  • Anyway I can fix this? – toy Feb 12 '16 at 15:09
  • See the linked duplicate and also [this question](http://stackoverflow.com/q/20279484/1048572) – Bergi Feb 12 '16 at 15:12

0 Answers0