gpt4 book ai didi

javascript - 关于使用 backbone.js 的 setTimeout 的各种问题。清除并恢复超时

转载 作者:行者123 更新时间:2023-11-30 13:09:56 24 4
gpt4 key购买 nike

我有很多关于 setTimeout 的问题:- 在我的代码中,我使用 clearTimeout(content.idTimeout) 为特定的 idTiemout 清除超时,但是所有超时的清除情况如何?我有下一个模型:

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

并清除 View 中的超时(片段):

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
}

- 当焦点位于其他窗口时如何清除超时并在返回时恢复?也许用焦点方法。下一段代码

$('html').focus(function() {
clearTimeout(content.idTimeout);
});

在 contentAdded 中不起作用。

编辑:

http://stackoverflow.com/questions/14258596/way-to-stop-the-running-of-a-javascript-web-application-when-the-focus-is-on-oth

最佳答案

一个解决方案可能是:

为您的(多个)超时 ID 指定一个数组。

window.timeouts = [];

每次调用 setTimeout 时:

timeouts.push(setTimeout(...));

然后,如果你想停止所有超时:

for(var i in timeouts) {
clearTimeout(timeouts[i]);
}

关于javascript - 关于使用 backbone.js 的 setTimeout 的各种问题。清除并恢复超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14257336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com