Friday, 15 May 2015

php - javascript timer not counting down when retrieving time from db -



php - javascript timer not counting down when retrieving time from db -

i trying follow jsfiddle illustration create time in javascript:

http://jsfiddle.net/g3rrj/

now timer in jsfiddle works fine. issue have time timer starts comes mysqli/php variable retrieves time db.

so except for:

<span id="countdown">01:30:10</span>

i have have as:

echo "<p><span id='countdown'>" . $dbsessionduration . "</span></p>";

and

except for:

var time = "01:30:10",

i have have as:

var time = <?php echo json_encode($dbsessionduration); ?>,

now getting no errors happening timer not doing count down. question why not counting down? illustration of time variable 01:00:00.

below code function:

echo "<p><span id='countdown'>" . $dbsessionduration . "</span></p>"; ... <script type="text/javascript"> (function(){ $(document).ready(function() { var time = <?php echo json_encode($dbsessionduration); ?>, parts = time.split(':'), hours = +parts[0], minutes = +parts[1], seconds = +parts[2], span = $('#countdown'); function correctnum(num) { homecoming (num<10)? ("0"+num):num; } var timer = setinterval(function(){ seconds--; if(seconds == -1) { seconds = 59; minutes--; if(minutes == -1) { minutes = 59; hours--; if(hours==-1) { alert("timer finished"); clearinterval(timer); return; } } } span.text(correctnum(hours) + ":" + correctnum(minutes) + ":" + correctnum(seconds)); }, 1000); }); }); </script>

change this:

}); }); </script>

to this:

}); })(); // ← note parentheses </script>

so call anonymous function. (alternatively, can remove (function(){ , }); entirely. there's no reason code in function @ all.)

php javascript jquery mysqli

No comments:

Post a Comment