0

我有这个来源。

$("#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;
});

在谷歌浏览器中,它不会立即评估脚本是否成功,但毕竟 ajax 调用它执行 las one。当我在成功中添加 alert() 时,它工作正常......在 firefox 中工作得很好。

4

3 回答 3

0

异步是一个被贬低的特性。成功也在路上。你应该使用

$.ajax({   
     type: "POST",
     data: formData,
     dataType: "html",  
     url: "/import/universalimport/"
     }).done(msg, textStatus, XMLHttpRequest) {
       ... rest of code when done here
于 2012-12-13T08:16:17.097 回答
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 响应乱序返回,就好像它们是在不同的时间间隔提交的一样)。这个模型是你说的问题吗?

于 2012-12-13T08:28:11.100 回答
0

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

于 2012-12-13T14:36:44.743 回答