0

这里是来源:

function updateMsg() {
    $.post("AjaxChatServer.jsp",{ time: timestamp }, function(xml) {
        alert("1");
        $("#loading").remove();
        addMessages(xml);
    });
    setTimeout('updateMsg()', 4000);
}

alert功能不起作用,但在 , 之外function(xml)起作用alert
jQuery源码有问题吗?

4

4 回答 4

3

改变:

setTimeout('updateMsg()', 4000);

至:

setTimeout(updateMsg, 4000);

另请注意,在发布的代码timestamp中,undefined您应该将setTimeout其放在函数范围之外。

于 2013-02-16T11:30:35.113 回答
2

如果您的成功回调没有触发,很可能是因为请求不成功。请参阅.post()文档。我建议使用功能更全面的.ajax()来更好地控制情况。

function updateMsg() {
    // You must define timestamp
    var timestamp = $.now();
    $.ajax({
        url: "AjaxChatServer.jsp",
        type: "POST",
        data: {
            time: timestamp
        },
        success: function (xml, textStatus, jqXHR) {
            $("#loading").remove();
            addMessages(xml);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            // Handle error
            alert(textStatus);
        }
    });
}

setTimeout(updateMsg, 4000);
于 2013-02-16T11:29:28.643 回答
0

我认为您可能对 ajax - 某个地方的错误有疑问。尝试在 post 函数的末尾添加.errorand.success以查看错误。

function updateMsg() {

$.post("AjaxChatServer.jsp",{ time: timestamp }, function(xml) {
  alert("1");
  $("#loading").remove();

 addMessages(xml);
})  .error(function(){
        alert("error");
     })  .success (function(){
        alert("error");
     );
于 2013-02-16T11:31:23.247 回答
0

您的变量“时间戳”已声明?

尝试 :

function updateMsg() {
    var date = new Date(), timestamp = date.getTime();

    $.post("AjaxChatServer.jsp",{ time: timestamp }, function(xml) {
       alert("1");
       $("#loading").remove();

       addMessages(xml);
    });
}
于 2013-02-16T11:38:36.777 回答