Skip to main content
update title to describe what code does, add tags
Link

Updating tag contents on my page Update game content via AJAX

edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

javascript loop and server side code Updating tag contents on my page

On my application, I have a javascriptJavaScript 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 Svc\Myapp\FrontHandler;

$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? There isIs there a way to refactor it to improve it?

javascript loop and server side code

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 Svc\Myapp\FrontHandler;

$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? There is a way to refactor it to improve it?

Updating tag contents on my page

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 Svc\Myapp\FrontHandler;

$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?

Source Link
Terix
  • 131
  • 3

javascript loop and server side code

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 Svc\Myapp\FrontHandler;

$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? There is a way to refactor it to improve it?