Ajax chrome不会立即成功评估脚本

时间:2012-12-13 08:10:23

标签: php javascript ajax jquery javascript-events

我有这个来源。

$("#allDataForm").submit( function (e) {
    e.stopPropagation();
    var formData = $("#allDataForm").serialize();
    var count = formData.substring(formData.indexOf('&count=')+7,formData.indexOf('&x='));
    var x = parseInt(formData.substring(formData.indexOf('&x=')+3, formData.length));
    if (formData.indexOf('=description&') > 0 && 
        formData.indexOf('=name&') > 0 && 
        formData.indexOf('=identifier&') > 0) {

        var head = 0;
        if (formData.indexOf('firstashead=on') > 0) {
            head=1;
        }
        head = parseInt(head);
        var imported = 0;
        var updated = 0;
        $("#assignTable").hide();
        $("#send").hide();
        $("#status").show();
        var totalTime = 0;
        if (count > 0) {
            for (s=x; s<=count; s++) {
                var startms = new Date().getTime();
                $.ajax({   
                    type: "POST",
                    data: formData,
                    dataType: "html",  
                    url: "/import/universalimport/",  
                    async: false, 
                    success: function(msg, textStatus, XMLHttpRequest) {
                        console.log($.makeArray(arguments), 'success');
                        console.log(textStatus, 'success1');
                        console.log(XMLHttpRequest, 'success2');
                        if (msg == 'imported') {
                            imported = parseInt(imported)+1;
                        } else if (msg == 'updated') {
                            updated = parseInt(updated)+1;
                        }
                        var endms = new Date().getTime();
                        totalTime = totalTime + (endms-startms);
                        x = totalTime / 1000;
                        tSeconds = Math.abs((x % 60).toFixed(0));
                        if (tSeconds < 10) {
                            tSeconds = 0+String(tSeconds);
                        }
                        x /= 60;
                        tMinutes = Math.abs((x % 60).toFixed(0));
                        if (tMinutes < 10) {
                            tMinutes = 0+String(tMinutes);
                        }
                        x /= 60;
                        tHours = Math.abs((x % 24).toFixed(0));
                        if (tHours < 10) {
                            tHours = 0+String(tHours);
                        }
                        x = (totalTime*(count-s-head)/s) / 1000;
                        aSeconds = Math.abs((x % 60).toFixed(0));
                        if (aSeconds < 10) {
                            aSeconds = 0+String(aSeconds);
                        }
                        x /= 60;
                        aMinutes = Math.abs((x % 60).toFixed(0));
                        if (aMinutes < 10) {
                            aMinutes = 0+String(aMinutes);
                        }
                        x /= 60;
                        aHours = Math.abs((x % 24).toFixed(0));
                        if (aHours < 10) {
                            aHours = 0+String(aHours);
                        }
                        eval($("#bar").css('width', (parseInt(s)/parseInt(count)*100).toFixed(2) + '%'));
                        $("#bar").html((parseInt(s)/parseInt(count)*100).toFixed(2) + '%');
                        $("#imported").html(imported);
                        $("#updated").html(updated);
                        $("#summary").html(imported+updated);
                        $("#count").html(count-head);
                        $("#elapsed").html(tHours + ':' + tMinutes + ':' + tSeconds);
                        $("#remaining").html(aHours + ':' + aMinutes + ':' + aSeconds);
                        formData = formData.substring(0, formData.indexOf('&x=')+3) + parseInt(s);
                    }
                });
            }
        }
    } else {
        alert('Pro provedení importu je nutno napárovat minimálně Název, Popis a Identifikátor!');
    }
    return false;
});

在Google Chrome中,它不会立即评估内部成功的脚本,但毕竟ajax调用后执行las。当我在成功中添加alert()时它工作得很好......在firefox中运行良好。

3 个答案:

答案 0 :(得分:0)

异步是一种折旧功能。成功也即将结束。你应该使用

$.ajax({   
     type: "POST",
     data: formData,
     dataType: "html",  
     url: "/import/universalimport/"
     }).done(msg, textStatus, XMLHttpRequest) {
       ... rest of code when done here

答案 1 :(得分:0)

这是一个jsfiddle,显示一组$ .ajax()POST一次性发送并以不同的间隔返回:

<ul id='log'></ul>
<script>
var call,
    log = $('#log');
for (call = 0; call < 10; call++) {
    $.ajax({
        type: 'POST',
        url: '/echo/html/',
        data: {
            html: "ajax response #" + call,
            delay: 3
        },
        success: function(data) {
            log.append('<li>'+data+'</li>')
            console.log(data);
        },
        dataType: 'html'
    });
    log.append('<li>'+('ajax request #' + call)+'</li>')
    console.log('ajax request #' + call);
}
</script>

我在Chrome和Firefox中运行此操作并且行为似乎相同(ajax响应按顺序返回,就像它们以不同的间隔提交一样)。这会模拟您所谈论的问题吗?

答案 2 :(得分:0)

通过递归调用异步ajax解决。谢谢你的帮助。