成功返回jQuery ajax后,继续每个迭代的工作

分为

在PHP中,我知道每个函数都可以返回true / false。JavaScript也是一样,如何$.each通过true / false操作jQuery迭代?我的意思是我该如何告诉迭代在得到响应之前保持不变?

现在我正在使用setTimeout()jQuery中的函数,但是我想替换为true / false响应并转到下一个迭代,有没有办法?

我的脚本是:

$(document).on( "click", '#sync',function(e) {
    e.preventDefault();
    $('#sync').prop('disabled', true);
    $.ajax({
            type: "POST",
            url:"./post/dataUrl",    
            data: {},
            dataType: 'json',
            success: function(data){
                if(data.status == 'success'){
                    console.log('Success, lets start the sync !');

                    $.each(data.data, function(i,item){
                        setTimeout(function() {
                            syncData(item.product_id);

                            var percentage = Math.ceil((i/data.total)*1000)/10;
                            $('#test').html('<div class="note note-success"><p>The sync is working... '+percentage+'% '+i+'/'+data.total+' <code>'+item.product_id+'</code></p></div>')

                            if(i == data.total - 1){
                                $('#sync').prop('disabled', false);
                                $('#test').empty();
                            }
                        }, i * 500); //So, in practic it runs after each 500ms, how can i replace this by standing on the response of syncData(product_id) ?
                    });
                    return false;
                }
            }
    });
});

function syncData(product_id){
    $.ajax({
            type: "POST",
            url:"./post/dataSave",    
            data: {product_id: product_id},
            dataType: 'json',
            success: function(data){
                if(data.status == 'success'){
                    return true;
                }
            }
    });
}
安德烈斯

您不能真正阻止每个循环迭代立即彼此立即运行。听起来更像是您希望回调触发下一项。我没有亲自测试过此代码,但是它应该给您一些提示。对于每个完成的syncData,它将同步下一个项目。

$(document).on( "click", '#sync',function(e) {
    e.preventDefault();
    $('#sync').prop('disabled', true);
    $.ajax({
            type: "POST",
            url:"./post/dataUrl",    
            data: {},
            dataType: 'json',
            success: function(data){
                if(data.status == 'success'){
                    console.log('Success, lets start the sync !');
                    function syncItem(i) {
                        var percentage = Math.ceil((i/data.total)*1000)/10;
                        $('#test').html('<div class="note note-success"><p>The sync is working... '+percentage+'% '+i+'/'+data.total+' <code>'+item.product_id+'</code></p></div>')
                        if (i == data.total) {
                            $('#sync').prop('disabled', false);
                            $('#test').empty();
                        } else {
                            var item = data.data[i];
                            syncData(item.product_id, function () {
                                // Here we make the next item sync
                                syncItem(i + 1);
                            }
                        }
                    }
                    if (data.total > 0) {
                        syncItem(0);
                    } else {
                        // handle empty data case
                    }
                }
            }
    });
});

function syncData(product_id, callback){
    $.ajax({
            type: "POST",
            url:"./post/dataSave",    
            data: {product_id: product_id},
            dataType: 'json',
            success: function(data){
                if(data.status == 'success'){
                    callback();
                }
            }
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery ajax返回成功数据

来自分类Dev

jQuery ajax返回成功数据

来自分类Dev

jQuery ajax成功$(this)后无法正常工作

来自分类Dev

jQuery ajax成功$(this)后无法正常工作

来自分类Dev

jQuery Ajax成功回调不触发也不返回错误

来自分类Dev

jQuery Ajax不会返回数据(解析错误),但是Postman成功

来自分类Dev

jQuery / Ajax返回成功消息并显示“已保存”指示符

来自分类Dev

jQuery Ajax成功获取返回的数组长度吗?

来自分类Dev

jQuery $ .ajax成功不返回任何东西?

来自分类Dev

jQuery.ajax,成功:函数(数据)不返回plainObject

来自分类Dev

jQuery Ajax成功获取返回的数组长度吗?

来自分类Dev

如何在jQuery Ajax成功返回的数组中进行遍历?

来自分类Dev

jQuery中的Ajax文件上传返回成功结果

来自分类Dev

ajax成功返回0

来自分类Dev

成功的ajax调用未返回成功功能

来自分类Dev

JQuery Ajax返回所有已触发的错误和成功消息;每个请求又一个

来自分类Dev

jQuery Ajax跨域返回成功作为错误函数中的状态

来自分类Dev

使用jQuery ajax发送到服务器的formData对象成功返回空数组

来自分类Dev

jQuery ajax成功范围

来自分类Dev

AJAX错误返回为成功

来自分类Dev

如何成功返回ajax结果?

来自分类Dev

jQuery未成功返回

来自分类Dev

ajax成功后的jQuery addClass / removeClass

来自分类Dev

jQuery Ajax在成功之前取消或取消成功

来自分类Dev

PHPMailer成功发送邮件但无法返回$.ajax成功部分

来自分类Dev

jQuery在Ajax上成功绑定

来自分类Dev

jquery ajax参数成功

来自分类Dev

jQuery Ajax失败...随机成功

来自分类Dev

jQuery ajax调用成功,然后