Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking progress of a PHP script with AJAX
    primarykey
    data
    text
    <pre><code> session_start(); if(isset($_GET['progress'])){ $status_done = '0'; if($_SESSION['progress_percentage'] == "100"){ $status_done = '1'; } echo json_encode(array( 'progress_percentage' =&gt; $_SESSION['progress_percentage'], 'progress_status' =&gt; $_SESSION['progress_status'], 'progress_done' =&gt; $status_done ) ); } elseif(isset($_GET['dummytask'])){ for ($i = 1; $i &lt;= 100; $i++) { $_SESSION['progress_percentage'] = $i; $_SESSION['progress_status'] = 'Checking the locks and stuffing the dolls!'; sleep(1); } } </code></pre> <pre><code> $(document).ready(function(){ var timeout = ''; function checkProgress(){ console.log('Checking progress event started'); $.ajax({ url: 'http://willetu54.fiftyfour.axc.nl/dgi/ajax.php?progress&amp;time='+ new Date().getTime(), cache: false, dataType: 'json' }).done(function(data) { $('#progressbar-progress').html(data.progress_status +'('+ data.progress_percentage +')'); console.log('Progress checked'); console.log(data); if(data.progress_done == "1"){ clearTimeout(timeout); } }).fail(function(jqXHR, status, error){ console.log('Check Progress: Error: '+ error + status +''); }); } function checkProgressTimeout(){ checkProgress(); console.log('Timeout tick for progress'); timeout = setTimeout(checkProgressTimeout, 500); } $('#menu-open').click(function(event){ checkProgressTimeout(); console.log('starting dummy task'); event.preventDefault(); $.get('http://willetu54.fiftyfour.axc.nl/dgi/ajax.php?dummytask', function(data) { alert('Load was performed.'); }); }); }); </code></pre> <p>Hello again Stackoverflow! </p> <p>The system I'm currently developing has scripts that run quite slow and can last up to 20 seconds sometimes. (Creating large amounts of folders, moving large amounts of files, etc). So thats why I've designed this script. I've created a small dummytask wich just lasts ~100 seconds. When I fire that script using AJAX it completes a certain amount of tasks. Every time a task completes I update my <code>$_SESSION</code> variable with the scripts' progress and load that onto my page with another jQuery script. </p> <p>But it works out kinda strange. I think it does launch the <code>checkProgressTimeout</code> and the dummy task (With the <code>checkProgressTimeout</code> function it checks the progress every 500 ms), But when I look in console I see it only fires the event but does not get any results untill the dummy task is completed and then it slowely returns the progress checking with the same timeval (500ms). </p> <p>This problem occurs in Google Chrome. The script doesn't work at all in IE.</p> <p>I've set up a jsFiddle: <a href="http://jsfiddle.net/nZAs4/2/" rel="nofollow">http://jsfiddle.net/nZAs4/2/</a>. But since jsFiddle does not support PHP scripts I've taken the privilege to upload it myself to one of my own webhosts. (I've also allowed Access-Control-Allow-Origin).</p> <p>So how can I get this to work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload