简体   繁体   English

setTimeout无法正常工作

[英]setTimeout doesn't work as expected

In my application I am loading user posts using the ajax scroll down feature. 在我的应用程序中,我正在使用ajax向下滚动功能加载用户帖子。

The for loop iteration takes too much time, browser freezes until the results are displayed. for循环迭代需要太多时间,浏览器将冻结直到显示结果。 So I implemented a setTimeout method to fix that, but for some reason the flow doesn't go inside the setTimeout method on debugging. 因此,我实现了setTimeout方法来解决该问题,但是由于某种原因,在调试时流程不会进入setTimeout方法内部。

Also the page is blank, data is not rendered. 页面也为空白,不呈现数据。

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

When the flow reaches setTimeout the next code it hits is the jquery lib 当流到达setTimeout时,它命中的下一个代码是jquery lib

在此处输入图片说明

Am I doing it right or missing something? 我做对了还是错过了什么?

Note: I get the responseJson data from the server fine. 注意:我从服务器端获得了responseJson数据。 Without the setTimeout the data is loaded on the page. 如果没有setTimeout,数据将被加载到页面上。

setTimeout takes an argument-less function ( https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout ), so having index as an argument is a little odd. setTimeout采用无参数函数( https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout ),因此将index作为参数有点奇怪。 I suspect that index is undefined so responseJson[index] is throwing out of bound exception (as evidenced by console.log(1) showing up as per Niloct's comment). 我怀疑索引是未定义的,所以responseJson[index]会抛出绑定异常(如根据Niloct的注释显示的console.log(1) )。 If you change your code to: 如果将代码更改为:

    $.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();
        });
    });

I suspect it will work. 我怀疑它会起作用。

(edited to take into account jjaulimsing's comment about not needing the encapsulating function.) (编辑时考虑到jjaulimsing关于不需要封装功能的评论。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM