2017-01-09 38 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