vicenrele
vicenrele

Reputation: 971

function in setTimeout() with Backbone.js

I am trying to use setTimeout() in a backbone model. The next code works: setTimeout(this.ajaxRequest,4000) but not the next: setTimeout(function(){ this.ajaxRequest; },4000);

Neither using _.bind(this.ajaxRequest, this); (Underscore.js) or

timeoutFunction: function(){ this.ajaxRequest; }, with setTimeout(this.timeoutFunction,4000);

setTimeout function is called when the AJAX response is done (success:) and _.bindAll(this); is executed in initialize:

Upvotes: 2

Views: 1243

Answers (1)

jeremy
jeremy

Reputation: 4314

You aren't calling the functions in the setTimeout(function) calls. For the first, you are passing a reference to a function which setTimeout is calling, the second instance you are passing a function and that function is doing nothing (nothing substantial). That function needs to call the function this.ajaxRequest()

var _this = this;
setTimeout(function(){ _this.ajaxRequest(); },4000);

Upvotes: 4

Related Questions