2012-12-13 37 views
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一个。当我添加alert()成功内部工作正常...在Firefox中工作良好。

回答

0

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

0

异步是折旧功能。成功也正在走出去。您应该使用

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

它不起作用。同样的问题。 –

0

下面是一个jsfiddle出一套$。阿贾克斯()发送一次全部职位和不同的时间间隔回来:

<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响应按照不同的时间间隔提交,但不按顺序返回)。这是否是你正在谈论的问题的模型?

+0

感谢它可能工作,但我不能浪费时间。这个ajax必须导入10000个产品,如果我能每3秒调用一次,它需要很长时间。当我导入图像需要更多时间,此功能停止工作。任何其他想法? –