在完成任务时,逐个显示ajax进程

时间:2017-08-22 05:32:36

标签: javascript php html ajax

我有一个代码,它有效,并且它同时显示所有通知。

我有疑问..

如何在完成其中一项功能后收到通知? (通知逐一出现)

indexing.php文件中有几个功能。

$preproses = $_POST["preproses"];
if($preproses == "preproses"){
    //mulai proses
    set_time_limit(0);
    buatindex();
    hitungbobot();
    panjangvektor();
}

function buatindex() {
    code
}

function hitungbobot() {
    code
}

function panjangvektor() {
    code
}

index.php中有一个代码来调用该函数

<script type="text/javascript">
function preproses(){

      var preprosesx = "preproses";
      $.ajax({
        type : "POST",
        url : "indexing.php",
        data: {preproses:preprosesx},
        error: function(){
        $("#notif").prepend("fail");
        },
        success: function(html){
        $("#notif").prepend("Process done <br/>"+html);
        },
      });
      return false;
  }
</script>

<a href="#" onclick="preproses()">click to precess</a>

如果所有流程都已完成,则会显示通知

<span id="notif"></span>

3 个答案:

答案 0 :(得分:1)

$preproses = $_POST["preproses"];
if($preproses == "preproses"){
    //mulai proses
   set_time_limit(0);

    setTimeout(function(){ buatindex() }, 3000);

   setTimeout(function(){ hitungbobot() }, 3000);

   setTimeout(function(){ panjangvektor() }, 3000);
   console.log("Completed all");
}

function buatindex() {
    code
}

function hitungbobot() {
    code
}

function panjangvektor() {
    code
}

您也可以在{ajax成功回复中console.log

像这样:JsFiddle Example

答案 1 :(得分:0)

计算你的post参数($ _POST [“preproses”])并保存到一个javascript变量,如var count =“”; 。您还可以采用隐藏的文本变量,每次发送通知后该变量将增加1。在所有通知值发送之后,此隐藏变量将等于count变量。然后您可以确保已发送所有通知。希望这对你有用.. :)

答案 2 :(得分:0)

你还没试过async: false吗?这将停止进一步处理,直到一个ajax请求完成。