Search code examples
javascriptphpajaxprogresspolling

How to make polling script using javasctipt and ajax


I have a script that runs a long conversion php script and a progress polling script. After looking at several posts about this subject i found that it should be possible to use async ajax calls combined with timeout from javascript to create a construction that would poll the progress regularly and update my page with a percentile number. See code below

function startExcelConversion(excelname){

var poll = function(){
   setTimeout(function(){
      $.ajax({ 
         url: "../include/ajax/ajax.php?action=poll_progress", 
         success: function(data){
             //Update the progress bar
            // show progress
            console.log('progresser: '+data);
            $("#progress").val(data);
             //Setup the next poll recursively
             poll();
         }, 
         complete: function( jqXHR, textStatus ){
             //Update the progress bar
            // show progress
            console.log(textStatus);
         },
         dataType: "json"
     });
  }, 3000);
};    

poll();

//show loading image
console.log('starting conversion');
$('#progress').val("Excel openen...");
$('#main').prepend('<img id="loading" src="../include/image/load.gif">');
$("#loading").show();

$.ajax({         
url: '../import/import_main.php?clean&action=importexcel&excelname='+excelname,         
success: function(data) {
    console.log(data);
    $("#main").html(data)
    $('#loading').hide();        
}     
});



return false;

}

the first block launches the script that runs a while (excel reading and conversion).This script updates a database table every 10 rows to set the progress. the second block (from start polling onwards0 should launch a php script that reads this progress db field and echo it so i can update my input field with the percentile. However the polling script is not called during the runtime of the first php script (import_main.php). I tried $.post and $.get calls (which should as $.ajax be asynchronous by default). Also tried setInterval but that did not work and was not recommended due to timing problems. Am i missing something obvious here or is it a setting in php i am missing?

thnx in advance


Solution

  • it turned out the problem was threefold:

    The first problem was indeed the use of an IIFE function that was not defined at runtime. So using inline function such as z416175 described was certainly valid

    The second problem was that when a session is active in PHP it will block other (ajax) calls to prevent session overwriting. So using session_write_close() before entering the long running script worked to allow asynchronous ajax calls for progress updating. See this post (thnx to z416175) One ajax call block other ajax call

    The third problem was that when you use xdebug the second problem remains because xdebug keeps a session open preventing the asynchronous ajax progress update call. So be aware when testing that xdebug causes problems with this

    Thanks for all input. I have credited z416175's post for various usefull info in his answer and comments