将ajax请求延迟x秒,并仅更新上次单击的请求

将ajax请求延迟x秒,并仅更新上次单击的请求,ajax,jquery,Ajax,Jquery,大家好 我将AJAX请求延迟3秒,然后用响应更新div。但我面临的问题是,当用户同时单击所有三个链接时,请求的内容在div中不断更新。我试图做的是,只提供最后一次单击的ajax请求内容,但不应该中止以前的请求。有什么办法可以实现吗??提前谢谢 剧本 HTML 您需要: 清除上次超时的方法 注册,并且 取消异步成功的一种方法 在超时事件中的函数 事件在启动之前未清除 已发出异步请求 对于第一项,可以使用带有setTimeout()返回的ID的clearTimeout()取消超时函数调

大家好 我将AJAX请求延迟3秒,然后用响应更新div。但我面临的问题是,当用户同时单击所有三个链接时,请求的内容在div中不断更新。我试图做的是,只提供最后一次单击的ajax请求内容,但不应该中止以前的请求。有什么办法可以实现吗??提前谢谢

剧本 HTML
您需要:

  • 清除上次超时的方法 注册,并且
  • 取消异步成功的一种方法 在超时事件中的函数 事件在启动之前未清除 已发出异步请求
对于第一项,可以使用带有setTimeout()返回的ID的clearTimeout()取消超时函数调用。。。很简单。至于第二点,您可以通过给每个调用添加时间戳,并将success函数中包含的timestamp变量的值与最后一次单击调用的时间戳进行比较来实现这一点

见下文:

// A tracker object outside the scope of all click functions
var pendingCall = { timeStamp: null, procID: null };

$('.links li a').click(function (e) {
    e.preventDefault();
    var getUrl = $(this).attr("href");

    // A timestamp for this call
    var timeStamp = Date.now();

    var printCall = function () {
        $.ajax({
            url: getUrl,
            type: "GET",
            beforeSend: function () { },
            error: function (request) { alert(request) },
            success: function (data) {
                // Short-circuit execution if the timestamp on this call doesn't match the last one made
                if (pendingCall.timeStamp != timeStamp) { return false; }

                // The action to take
                $('#graphContent').html(data);

                // Clear the reference to this timeout call
                pendingCall.procID = null;
            }
        });
    };

    // Clear the timeout on the last call made if it exists
    if (pendingCall.procID) {
        clearTimeout(pendingCall.procID)
    };

    // Update the timeout call tracker
    pendingCall = { timeStamp: timeStamp, procID: setTimeout(printCall, 3000) };
});

我猜你的三只手三只鼠标用户在计时方面很在行吧?无论如何为什么不想中止任何正在运行的
printCall
函数?
<ul>
<li><a href="http://localhost/test.php">Link 1</a></li>
<li><a href="http://localhost">Link 2</a></li>
<li><a href="index.html">Link 3</a></li>
</ul>
// A tracker object outside the scope of all click functions
var pendingCall = { timeStamp: null, procID: null };

$('.links li a').click(function (e) {
    e.preventDefault();
    var getUrl = $(this).attr("href");

    // A timestamp for this call
    var timeStamp = Date.now();

    var printCall = function () {
        $.ajax({
            url: getUrl,
            type: "GET",
            beforeSend: function () { },
            error: function (request) { alert(request) },
            success: function (data) {
                // Short-circuit execution if the timestamp on this call doesn't match the last one made
                if (pendingCall.timeStamp != timeStamp) { return false; }

                // The action to take
                $('#graphContent').html(data);

                // Clear the reference to this timeout call
                pendingCall.procID = null;
            }
        });
    };

    // Clear the timeout on the last call made if it exists
    if (pendingCall.procID) {
        clearTimeout(pendingCall.procID)
    };

    // Update the timeout call tracker
    pendingCall = { timeStamp: timeStamp, procID: setTimeout(printCall, 3000) };
});