Update game content via AJAX On my application, I have a JavaScript cycle that updates tag contents on my page regularly:

&lt;script type="text/javascript"&gt;
    var players = [];
    players.push($('#p0'));
    players.push($('#p1'));
    players.push($('#p2'));
    players.push($('#p3'));
    i=0;
    /*
     * cycle through the players.
     */
    function cyclePlayer() {
        players[i].parents(".card-header").addClass('border-success');
        players[i].load('play.php?p='+i);
        i = ++i % players.length;
        $('#deck').load('play.php?deck=1');
        $('#pile').load('play.php?pile=1');
        $('#feedback').load('play.php?feedback=1');
    }
    PlayerLoop = setInterval('cyclePlayer()', 1500 );

    $("#stop").click(function(){
        clearInterval(PlayerLoop);
    });

    $('#reset').click(function() {
        $('#deck').load('play.php?reset=1');
        location.reload();
    });
&lt;/script&gt;


The server code that is being called looks like this:

&lt;?php
require '../vendor/autoload.php';

session_start();

use Svc\Myapp\FrontHandler;

$myApp = new FrontHandler();

if (isset($_GET['p']) &amp;&amp; !isset($_SESSION['winner'])) {
    echo $myApp-&gt;playerTurn();
}

if (isset($_GET['deck'])) {
    echo $myApp-&gt;deckUpdate();
}

if (isset($_GET['pile'])) {
    echo $myApp-&gt;pileUpdate();
}

if (isset($_GET['feedback'])) {
    echo $myApp-&gt;feedbackUpdate();
}


Is this production quality code? Is there a way to refactor it to improve it?
My answer focuses on the ajax requests.

Why 4 requests at each 1500 milliseconds to the same file (play.php)?

I would do only one request and I would "cycle" on a delay starting from the success callback... instead of a fixed interval, which does not take the request delay in account.

var players = [ // No need to use a push method when you already know the array content.
  $('#p0'),
  $('#p1'),
  $('#p2'),
  $('#p3')
];

i=0;

function cyclePlayer() {

  $.ajax({
    url: "play.php",
    data: {iteration:i},
    method: "GET",
    dataType: "json",
    success: (data)=&gt;{

      console.log(JSON.stringify(data));  // See all the infos in the same request response.
      json = JSON.parse(data);

      players[i].html(json.playerTurn).parents(".card-header").addClass('border-success');

      i = ++i % players.length;

      $('#deck').html(json.deckUpdate);
      $('#pile').html(json.pileUpdate);
      $('#feedback').html(json.feedbackUpdate);

      PlayerLoop = setTimeout(cyclePlayer,1500);    // From here, set a new 1500ms timeout.
    },
    error: ()=&gt;{

    },
  });
}

// on load, start a timeout.
var PlayerLoop = setTimeout(cyclePlayer, 1500 );

$("#stop").click(function(){
    clearTimeout(PlayerLoop);
});

$('#reset').click(function() {
  location.reload(true);  // true is to reload from server instead of possibly loading from cache.
});


And the PHP:

&lt;?php
require '../vendor/autoload.php';
session_start();
use Svc\Myapp\FrontHandler;

if(!isset($_GET['iteration'])){die();}


$myApp= new FrontHandler();

if(!isset($_SESSION['winner'])){
  $results["playerTurn"] = $myApp-&gt;playerTurn();
}
$results["deckUpdate"] = $myApp-&gt;deckUpdate();
$results["pileUpdate"] = $myApp-&gt;pileUpdate();
$results["feedbackUpdate"] = $myApp-&gt;feedbackUpdate();

$results["isSetWinner"] = isset($_SESSION['winner']);

// Echo the array as a json. This will be the response of the request.
echo json_encode($result);
?&gt;


Now, I could not test the above... I hope It does not have any typo.