Note that there are some explanatory texts on larger screens.

plurals
  1. POvarious questions about setTimeout using backbone.js. Clean and resume timeouts
    text
    copied!<p>I have various questions about setTimeout: - In my code, I clean timeouts with <code>clearTimeout(content.idTimeout)</code> for a particular idTiemout, but how clear all timeouts? I have the next model:</p> <pre><code>var ContentModel = Backbone.Model.extend({ URL: "http://localhost/example.php", requestType: "POST", dataType: "json", data: "", //Set the value outside the model idTimeout: "", initialize: function() { _.bindAll(this); }, startSend: function (Data) { }, reply: function (Data) { var dataJson = eval(Data); console.log(dataJson); this.idTimeout = setTimeout(this.ajaxRequest(),4000); }, problems: function (Data) { }, ajaxRequest: function () { $.ajax({ async:true, type: this.requestType, dataType: this.dataType, url: this.URL, data: this.data, beforeSend:this.startSend, success: this.reply, timeout:4000, error:this.problems }); } </code></pre> <p>And clean timeouts in the view (fragment):</p> <pre><code>initialize: function() { _.bindAll(this); this.model = new ContentCollection(); this.model.on("add", this.contentAdded); this.model.on("remove", this.removeContent); }, contentAdded: function(content) { //run it when add a model if (content.view == null) { var template_name = 'cam'; content.view = new ContentView({model: content,template: $.trim($("[template='"+ template_name +"'] div").html() || "Template not found!")}); $("div.camBox").empty(); content.ajaxRequest(); this.$el.find(".content").find("div.camBox").append(content.view.render().el); } }, removeContent: function(content) { if (content.view != null) { content.view.remove(); } clearTimeout(content.idTimeout); content.clear(); //Clear the properties of the model } </code></pre> <p>- How clean a timeout when the focus is in other window and resume it when returns? Maybe with focus method. The next code </p> <pre><code>$('html').focus(function() { clearTimeout(content.idTimeout); }); </code></pre> <p>in contentAdded does not work.</p> <p>Edit: </p> <p>http://stackoverflow.com/questions/14258596/way-to-stop-the-running-of-a-javascript-web-application-when-the-focus-is-on-oth</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload