2017-01-09 40 views
1

任何人都可以幫我使用這段代碼嗎?我想將這個異步ajax轉換爲同步ajax。我該怎麼做?這裏是下面寫的代碼。當我離開它20分鐘甚至沒有做任何事情時,系統就滯後了。所以,也許問題在代碼中。MySQL AJAX PHP通知模塊

function addmsg20(type, msg, data) { 
     $('#display20').html(data); 

    } 

    function waitForMsg20() { 
     $.ajax({ 
      type: "GET", 
      url: "liprintednotif.php", 
      async: true, 
      cache: false, 
      timeout: 50000, 
      success: function(data) { 
       var data = JSON.parse(data); 
       if(data.data.length > 0){ 
        var res = data.data; 
        var printed = "<ul class='menu'>"; 
       $.each(res, function(k, v){ 
        var empext = v.employee_ext; 
        if(empext == null){ 
         empext = ""; 
        }else{ 
         empext = v.employee_ext; 
        } 
        printed += "<li>" +    
         "<a href='#'>" + 
          "<h4>" + 
          " "+v.employee_fname + " " +       
           v.employee_mname+" "+v.employee_lname+" " +empext+ "<small>" + 
           v.status_desc+ "</small>" +"</h4>" + 
          "<p>" + v.subj_name + "</p>" + 
         "</a></li>"; 

       }) 
       printed += "</ul>"; 
       }else{ 
        printed = ""; 
       } 
       addmsg20("new", data.count, printed); 
       setTimeout(
        waitForMsg20, 
        1000); 
      }, 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
       addmsg20("error", textStatus + " (" + errorThrown + ")"); 
       setTimeout(
        waitForMsg20, 
        15000); 
      } 
     }); 
    }; 

    $(document).ready(function() { 

     waitForMsg20(); 

    }); 

我完全不熟悉JavaScript和/或AJAX,所以我真的需要幫助。

+0

如果你不希望它是異步的,你可以把「異步」選項設置爲false – Phiter

+0

我想已經,它不起作用。 – JayPii

+0

主線程上的同步XMLHttpRequest由於其對最終用戶體驗的不利影響而不推薦使用。如需更多幫助,請查看https://xhr.spec.whatwg.org/。 – JayPii

回答

0

爲了使AJAX調用同步,只需設置asyncfalse

$.ajax({ 
     type: "GET", 
     url: "liprintednotif.php", 
     async: false, 
     cache: false, 
     timeout: 50000, 
     . 
     . 
     . 
+0

它在控制檯中顯示了這一點: 主線程上的同步XMLHttpRequest由於其對最終用戶體驗的不利影響而不推薦使用。如需更多幫助,請查看https://xhr.spec.whatwg.org/。 – JayPii