如何停止当前的ajax连接并启动一个新连接?

时间:2022-01-13 01:17:52

i have a chat box that will load new messages using ajax on my content page and it will listen for new ones every 20s. It is working. However, right now i have another link that will allow a user to load previous messages. And that button does not seem to work even though i have already done a clearTimeout() on the existing ajax function. any idea why?

我有一个聊天框,它将在我的内容页面上使用ajax加载新消息,它将每隔20秒收听一个新消息。这是工作。但是,现在我有另一个链接,允许用户加载以前的消息。即使我已经在现有的ajax函数上完成了clearTimeout(),该按钮似乎也不起作用。知道为什么吗?

My scripts:

我的脚本:

   //for loading previous messages

    var timer;       

    $('#getoldmessages').click(function() {

        $("#workroom_boxes_chatpast").html('<img src="{{ MEDIA_URL }}images/ui-anim_basic_16x16.gif"/>&nbsp; Loading...');
        $.ajax({
            url: "/messages/{{ chat.key.id }}",
            cache: false,
            success: function(html){
                $("#chatcontent").html(html);
                $("#workroom_boxes_chatpast").html('Loaded');
            }
        });
                    clearTimeout(timer);

});      



   //for new messages

function updateMsg() {
$.ajax({
url: "/recent/messages/{{ chat.key.id }}",
cache: false,
success: function(html){
$("#chatcontent").html(html);
}
});
//alert('repeating');
t = setTimeout(updateMsg, 20000);
}
updateMsg();

1 个解决方案

#1


1  

setTimeout(this, 20000); is broken, this refers to a jquery object in that context, not a function. Check your javascript console and you'll see an error.

setTimeout(this,20000);如果被破坏,则指的是该上下文中的jquery对象,而不是函数。检查你的JavaScript控制台,你会看到一个错误。

You don't need to clearTimeout to make a separate ajax call. Moreover, your call might not work because you're referring to the variable timer which has the timeout id from the first call to setTimeout, but any successive call will return a new id. So you need to add in your updateMsg the variable assignment: timer = setTimeout(updateMsg,20000);.

您不需要clearTimeout来进行单独的ajax调用。此外,您的调用可能不起作用,因为您指的是第一次调用setTimeout时具有超时ID的变量计时器,但任何连续调用都将返回一个新的id。所以你需要在updateMsg中添加变量赋值:timer = setTimeout(updateMsg,20000);.

If your initial code is being executed in a $() block separate from updateMsg, then timer will only be visible in that block and changing that in updateMsg won't have an effect, another problem. If so, either make timer global, or attach it to a global object, or put updateMsg in that $() block.

如果您的初始代码是在与updateMsg分开的$()块中执行的,那么timer只会在该块中可见,并且在updateMsg中更改它不会产生影响,这是另一个问题。如果是这样,要么使计时器全局,要么将其附加到全局对象,或将updateMsg放在该$()块中。

Lastly, you should be using setInterval anyway, instead of recalling setTimeout every time, that's what it is made for.

最后,你应该使用setInterval,而不是每次都调用setTimeout,这就是它的用途。

#1


1  

setTimeout(this, 20000); is broken, this refers to a jquery object in that context, not a function. Check your javascript console and you'll see an error.

setTimeout(this,20000);如果被破坏,则指的是该上下文中的jquery对象,而不是函数。检查你的JavaScript控制台,你会看到一个错误。

You don't need to clearTimeout to make a separate ajax call. Moreover, your call might not work because you're referring to the variable timer which has the timeout id from the first call to setTimeout, but any successive call will return a new id. So you need to add in your updateMsg the variable assignment: timer = setTimeout(updateMsg,20000);.

您不需要clearTimeout来进行单独的ajax调用。此外,您的调用可能不起作用,因为您指的是第一次调用setTimeout时具有超时ID的变量计时器,但任何连续调用都将返回一个新的id。所以你需要在updateMsg中添加变量赋值:timer = setTimeout(updateMsg,20000);.

If your initial code is being executed in a $() block separate from updateMsg, then timer will only be visible in that block and changing that in updateMsg won't have an effect, another problem. If so, either make timer global, or attach it to a global object, or put updateMsg in that $() block.

如果您的初始代码是在与updateMsg分开的$()块中执行的,那么timer只会在该块中可见,并且在updateMsg中更改它不会产生影响,这是另一个问题。如果是这样,要么使计时器全局,要么将其附加到全局对象,或将updateMsg放在该$()块中。

Lastly, you should be using setInterval anyway, instead of recalling setTimeout every time, that's what it is made for.

最后,你应该使用setInterval,而不是每次都调用setTimeout,这就是它的用途。