JS PHP

AJAX LONG POLLING - A SIMPLE EXAMPLE

var last = 0; LAST UPDATE function poll () {   SEND LAST TIMESTAMP   let data = new FormData();   data.append("last", last);

LONG POLL AJAX CALL

01

  LOOP FETCH   fetch("SCRIPT.PHP",   { method:"POST", body:data })   .then(res => res.json()).then((data) => {     last = data.time; poll();   }).catch((e) => { poll(); }); }

STOP ON DISCONNECT set_time_limit(30); ignore_user_abort(false);

PHP UPDATE CHECK LOOP

02

while (true) {   GET UPDATE + RESPOND   $udpate = getupdate();   if ($update["time"] > $_POST["last"])   { echo json_encode($update); break; }     SHORT PAUSE   sleep(1); }