jquery - 双 jQuery 延迟() 的问题

标签 jquery post delay

我正在摆弄 jQuery 的 AJAX 函数,并试图模拟真实的服务器如何延迟我在本地主机上获得的顺利数据请求。

所以我编写了与此类似的代码:

$('form').submit(function (event) {
    event.preventDefault();

$('#response').html('<p>Posting...</p>').fadeIn().delay(2000).queue(function () {
        $.post (
        'some_url.php', 
        {/*values here*/},
        function (response) {
            $('#response').html(response).delay(1000).fadeOut('slow');

            //The line below is to reset the form element
            $('input[type="text"], textarea').val(' ');
        });
    });
});

我在这里基本上所做的是将 $.post 方法延迟 2 秒,以便可以看到“Posting...”消息。 当 2 秒结束时,我希望文本根据我得到的响应进行更改,保持静止 1 秒,并且我希望它淡出。

第一个延迟工作完美,Ajax 调用也工作完美,问题是 - 由于某种原因,第二个延迟没有被读取,并且响应消息一旦显示,就拒绝消失:)

我的问题是为什么会发生这种情况以及如何解决它?

最佳答案

您需要出队才能处理队列中的下一个事物。

http://api.jquery.com/jQuery.dequeue/

编辑:或者 1.4 或更高版本中的下一个:

In jQuery 1.4 the function that's called is passed in another function, as the first argument, that when called automatically dequeues the next item and keeps the queue moving. You would use it like so:

$("#test").queue(function(next) {
     // Do some stuff...
     next(); 
});

来自:http://api.jquery.com/queue/

关于jquery - 双 jQuery 延迟() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5941172/

相关文章:

javascript - 每次递归调用函数之间的延迟

每张幻灯片上的 jQuery Cycle 全宽包装器

javascript - 检查 JSON 中是否存在值

python - 使用 Flask 以更好的方式处理带有可变信息的 POST 请求

php - 如何在使用 filter_input 的同时检查 POST 和 GET 数据?

c - OpenCL 内核等待/延迟

javascript - 相似元素互不干扰

jquery - 如何将 select2 append 到 div 框?

java - Http Post - 通信失败 - 为什么?

actionscript - 在 ActionScript 行之间暂停