Updating tag contents on my page

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












On my application, I have a JavaScript cycle that updates tag contents on my page regularly:



<script type="text/javascript">
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();
);
</script>


The server code that is being called looks like this:



<?php
require '../vendor/autoload.php';

session_start();

use SvcMyappFrontHandler;

$myApp = new FrontHandler();

if (isset($_GET['p']) && !isset($_SESSION['winner']))
echo $myApp->playerTurn();


if (isset($_GET['deck']))
echo $myApp->deckUpdate();


if (isset($_GET['pile']))
echo $myApp->pileUpdate();


if (isset($_GET['feedback']))
echo $myApp->feedbackUpdate();



Is this production quality code? Is there a way to refactor it to improve it?







share|improve this question





















  • Considering your <?php tag never closes, I would say no.
    – FreezePhoenix
    Jul 29 at 16:29






  • 3




    @FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
    – Terix
    Jul 29 at 19:43










  • This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
    – chicks
    Jul 30 at 2:40










  • I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
    – jrswgtr
    Aug 1 at 19:09

















up vote
1
down vote

favorite












On my application, I have a JavaScript cycle that updates tag contents on my page regularly:



<script type="text/javascript">
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();
);
</script>


The server code that is being called looks like this:



<?php
require '../vendor/autoload.php';

session_start();

use SvcMyappFrontHandler;

$myApp = new FrontHandler();

if (isset($_GET['p']) && !isset($_SESSION['winner']))
echo $myApp->playerTurn();


if (isset($_GET['deck']))
echo $myApp->deckUpdate();


if (isset($_GET['pile']))
echo $myApp->pileUpdate();


if (isset($_GET['feedback']))
echo $myApp->feedbackUpdate();



Is this production quality code? Is there a way to refactor it to improve it?







share|improve this question





















  • Considering your <?php tag never closes, I would say no.
    – FreezePhoenix
    Jul 29 at 16:29






  • 3




    @FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
    – Terix
    Jul 29 at 19:43










  • This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
    – chicks
    Jul 30 at 2:40










  • I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
    – jrswgtr
    Aug 1 at 19:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











On my application, I have a JavaScript cycle that updates tag contents on my page regularly:



<script type="text/javascript">
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();
);
</script>


The server code that is being called looks like this:



<?php
require '../vendor/autoload.php';

session_start();

use SvcMyappFrontHandler;

$myApp = new FrontHandler();

if (isset($_GET['p']) && !isset($_SESSION['winner']))
echo $myApp->playerTurn();


if (isset($_GET['deck']))
echo $myApp->deckUpdate();


if (isset($_GET['pile']))
echo $myApp->pileUpdate();


if (isset($_GET['feedback']))
echo $myApp->feedbackUpdate();



Is this production quality code? Is there a way to refactor it to improve it?







share|improve this question













On my application, I have a JavaScript cycle that updates tag contents on my page regularly:



<script type="text/javascript">
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();
);
</script>


The server code that is being called looks like this:



<?php
require '../vendor/autoload.php';

session_start();

use SvcMyappFrontHandler;

$myApp = new FrontHandler();

if (isset($_GET['p']) && !isset($_SESSION['winner']))
echo $myApp->playerTurn();


if (isset($_GET['deck']))
echo $myApp->deckUpdate();


if (isset($_GET['pile']))
echo $myApp->pileUpdate();


if (isset($_GET['feedback']))
echo $myApp->feedbackUpdate();



Is this production quality code? Is there a way to refactor it to improve it?









share|improve this question












share|improve this question




share|improve this question








edited Jul 30 at 2:59









Jamal♦

30.1k11114225




30.1k11114225









asked Jul 29 at 16:17









Terix

1063




1063











  • Considering your <?php tag never closes, I would say no.
    – FreezePhoenix
    Jul 29 at 16:29






  • 3




    @FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
    – Terix
    Jul 29 at 19:43










  • This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
    – chicks
    Jul 30 at 2:40










  • I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
    – jrswgtr
    Aug 1 at 19:09

















  • Considering your <?php tag never closes, I would say no.
    – FreezePhoenix
    Jul 29 at 16:29






  • 3




    @FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
    – Terix
    Jul 29 at 19:43










  • This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
    – chicks
    Jul 30 at 2:40










  • I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
    – jrswgtr
    Aug 1 at 19:09
















Considering your <?php tag never closes, I would say no.
– FreezePhoenix
Jul 29 at 16:29




Considering your <?php tag never closes, I would say no.
– FreezePhoenix
Jul 29 at 16:29




3




3




@FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
– Terix
Jul 29 at 19:43




@FreezePhoenix Not closing the PHP tag is actually good practice, if the file has just PHP in it. stackoverflow.com/questions/3219383/…
– Terix
Jul 29 at 19:43












This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
– chicks
Jul 30 at 2:40




This still doesn't fit the site's guidelines. It isn't clear what you're trying to do with the code. And the title should be more descriptive and less prescriptive. codereview.stackexchange.com/help/how-to-ask
– chicks
Jul 30 at 2:40












I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
– jrswgtr
Aug 1 at 19:09





I am only commenting on style now. Variables should be defined with a keyword like var, const or let. They should start with a lowercase character. PlayerLoop = setInterval('cyclePlayer()', 1500 ); is a nono. Define it with var or let like this let playerLoop = setInterval('cyclePlayer()', 1500 );.
– jrswgtr
Aug 1 at 19:09











1 Answer
1






active

oldest

votes

















up vote
1
down vote













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)=>

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: ()=>

,
);


// 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:



<?php
require '../vendor/autoload.php';
session_start();
use SvcMyappFrontHandler;

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


$myApp= new FrontHandler();

if(!isset($_SESSION['winner']))
$results = ["playerTurn"] = $myApp->playerTurn();

$results = ["deckUpdate"] = $myApp->deckUpdate();
$results = ["pileUpdate"] = $myApp->pileUpdate();
$results = ["feedbackUpdate"] = $myApp->feedbackUpdate();

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

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


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






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200528%2fupdating-tag-contents-on-my-page%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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)=>

    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: ()=>

    ,
    );


    // 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:



    <?php
    require '../vendor/autoload.php';
    session_start();
    use SvcMyappFrontHandler;

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


    $myApp= new FrontHandler();

    if(!isset($_SESSION['winner']))
    $results = ["playerTurn"] = $myApp->playerTurn();

    $results = ["deckUpdate"] = $myApp->deckUpdate();
    $results = ["pileUpdate"] = $myApp->pileUpdate();
    $results = ["feedbackUpdate"] = $myApp->feedbackUpdate();

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

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


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






    share|improve this answer

























      up vote
      1
      down vote













      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)=>

      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: ()=>

      ,
      );


      // 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:



      <?php
      require '../vendor/autoload.php';
      session_start();
      use SvcMyappFrontHandler;

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


      $myApp= new FrontHandler();

      if(!isset($_SESSION['winner']))
      $results = ["playerTurn"] = $myApp->playerTurn();

      $results = ["deckUpdate"] = $myApp->deckUpdate();
      $results = ["pileUpdate"] = $myApp->pileUpdate();
      $results = ["feedbackUpdate"] = $myApp->feedbackUpdate();

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

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


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






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        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)=>

        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: ()=>

        ,
        );


        // 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:



        <?php
        require '../vendor/autoload.php';
        session_start();
        use SvcMyappFrontHandler;

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


        $myApp= new FrontHandler();

        if(!isset($_SESSION['winner']))
        $results = ["playerTurn"] = $myApp->playerTurn();

        $results = ["deckUpdate"] = $myApp->deckUpdate();
        $results = ["pileUpdate"] = $myApp->pileUpdate();
        $results = ["feedbackUpdate"] = $myApp->feedbackUpdate();

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

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


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






        share|improve this answer













        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)=>

        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: ()=>

        ,
        );


        // 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:



        <?php
        require '../vendor/autoload.php';
        session_start();
        use SvcMyappFrontHandler;

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


        $myApp= new FrontHandler();

        if(!isset($_SESSION['winner']))
        $results = ["playerTurn"] = $myApp->playerTurn();

        $results = ["deckUpdate"] = $myApp->deckUpdate();
        $results = ["pileUpdate"] = $myApp->pileUpdate();
        $results = ["feedbackUpdate"] = $myApp->feedbackUpdate();

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

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


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







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 31 at 17:43









        Louys Patrice Bessette

        1417




        1417






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200528%2fupdating-tag-contents-on-my-page%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation