Javascript 设置超时不';I don’我没有按预期工作
在我的应用程序中,我使用ajax向下滚动功能加载用户帖子 for循环迭代花费的时间太长,浏览器将冻结,直到显示结果。因此,我实现了一个setTimeout方法来解决这个问题,但是由于某些原因,调试时流没有进入setTimeout方法 此外,页面为空白,不呈现数据Javascript 设置超时不';I don’我没有按预期工作,javascript,jquery,ajax,Javascript,Jquery,Ajax,在我的应用程序中,我使用ajax向下滚动功能加载用户帖子 for循环迭代花费的时间太长,浏览器将冻结,直到显示结果。因此,我实现了一个setTimeout方法来解决这个问题,但是由于某些原因,调试时流没有进入setTimeout方法 此外,页面为空白,不呈现数据 success : function(responseJson) { $("#loadingdata").toggle(); enableScrolling(); if($.isEm
success : function(responseJson) {
$("#loadingdata").toggle();
enableScrolling();
if($.isEmptyObject(responseJson)){
$("#noMorePosts").toggle();
disableScrolling();
paginationComplete=true;
}
$.each(responseJson, function (index) {
(function(index) {
setTimeout(function(index) { //the flow doesn't move inside this
var resp_JSON=responseJson[index];
var dateObj=resp_JSON.postCreationTime;
resp_JSON.postCreationTime = moment(dateObj).format("h:mm a, ddd, MMM Do YY");
var timeago = moment(dateObj).fromNow();
resp_JSON.timeago = timeago;
resp_JSON.username=userName;
var post_template = $('#homepostcontainertemplate').html();
Mustache.parse(post_template);
var post_info = Mustache.to_html(post_template, resp_JSON);
$('#homepublisherpostsdiv').append(post_info);
$('div').linkify();
});
})(index);
});
当流达到setTimeout时,它遇到的下一个代码是jquery库
我做对了还是遗漏了什么
注意:我很好地从服务器获取了响应数据。如果没有setTimeout,数据将加载到页面上。setTimeout采用一个无参数函数(),因此将
index
作为参数有点奇怪。我怀疑索引没有定义,所以responseJson[index]
抛出了越界异常(根据Niloct的评论显示console.log(1)
)。如果您将代码更改为:
$.each(responseJson, function (index) {
setTimeout(function() { // no index in the argument list
var resp_JSON=responseJson[index];
var dateObj=resp_JSON.postCreationTime;
resp_JSON.postCreationTime = moment(dateObj).format("h:mm a, ddd, MMM Do YY");
var timeago = moment(dateObj).fromNow();
resp_JSON.timeago = timeago;
resp_JSON.username=userName;
var post_template = $('#homepostcontainertemplate').html();
Mustache.parse(post_template);
var post_info = Mustache.to_html(post_template, resp_JSON);
$('#homepublisherpostsdiv').append(post_info);
$('div').linkify();
});
});
我怀疑它会起作用
(编辑时考虑了Jjauliming关于不需要封装函数的评论。)setTimeout采用了一个无参数函数(),因此将
索引作为参数有点奇怪。我怀疑索引没有定义,所以responseJson[index]
抛出了越界异常(根据Niloct的评论显示console.log(1)
)。如果您将代码更改为:
$.each(responseJson, function (index) {
setTimeout(function() { // no index in the argument list
var resp_JSON=responseJson[index];
var dateObj=resp_JSON.postCreationTime;
resp_JSON.postCreationTime = moment(dateObj).format("h:mm a, ddd, MMM Do YY");
var timeago = moment(dateObj).fromNow();
resp_JSON.timeago = timeago;
resp_JSON.username=userName;
var post_template = $('#homepostcontainertemplate').html();
Mustache.parse(post_template);
var post_info = Mustache.to_html(post_template, resp_JSON);
$('#homepublisherpostsdiv').append(post_info);
$('div').linkify();
});
});
我怀疑它会起作用
(编辑时考虑了Jjauliming关于不需要封装功能的评论。)console.log(1)
在setTimeout
回调的第一行之前,它是否在控制台中打印?我将它放在setTimeout之前,在(函数(索引)之后{;它确实在控制台中打印了10次1;但它也显示了Uncaught TypeError:无法读取未定义的10次属性'postCreationTime'。可能setTimeout内的代码无法读取响应。这是使用$.each中的封装函数的特殊原因吗?它与直接写入$.each有何不同(responseJson,function(index){setTimeout(function(){…我从中获得了代码,问题是每次迭代时resp_JSON变量都没有定义。console.log(1);
在setTimeout
回调的第一行之前,它是否在控制台中打印?我把它放在setTimeout之前,在(function(index)之后{;它确实在控制台中打印了10次1;但它也显示了Uncaught TypeError:无法读取未定义的10次属性'postCreationTime'。可能setTimeout内的代码无法读取响应。这是使用$.each中的封装函数的特殊原因吗?它与直接写入$.each有何不同(responseJson,函数(索引){setTimeout(函数(){…我从中获得了代码,问题是每次迭代时resp_JSON变量都未定义。嗨,Syaz,请接受用户名编辑请求。我正在尝试从搜索引擎中删除引用。提前谢谢。嗨,Syaz,请接受用户名编辑请求。我正在尝试从搜索引擎中删除引用引擎,谢谢。