1

我的脚本运行良好。但是,内容不会自行刷新以获取新数据。为什么会这样?

 function updateMsg() {
    $.ajax({
       url: "/recent/notifications/",
       cache: false,
       success: function(html){     
         $("#profile_notifarea_msgbox").html(html);
       }
    });
    setTimeout('updateMsg()', 4000);
 }
 updateMsg();   
4

2 回答 2

1

您的 setTimeout 可以直接引用 updateMsg 而不是使用字符串:

var timeout;

function updateMsg() {
   $.ajax({
      url: "/recent/notifications/",
      cache: false,
      success: function(html){     
        $("#profile_notifarea_msgbox").html(html);
        timeout = setTimeout(updateMsg, 4000);
      }
   });       
}
updateMsg();   

function stopUpdate() {
    clearTimeout(timeout);
}

要停止持续更新,您可以在变量中保存对 setTimeout 的引用,然后调用 clearTimeout 并传入该变量。在此示例中,您只需调用函数 stopUpdate() 即可取消更新。

于 2011-03-25T15:53:39.720 回答
0

当您将 ajax 与 jQuery 一起使用时,请尝试始终放置错误函数,这样您就可以确定请求是否有问题

于 2011-03-25T16:27:12.190 回答