Reputation: 482
I have been at this for quite a while now but can't seem to crack why this javascript timer, using PHP variables will not actually count down...
Here is my code
<script>
<?php
if($_SESSION['mydropdown'] == 1){
$var = ($_SESSION['timer'] + 0.1 * 60) - time();
}
else {
$var = ($_SESSION['timer'] + 1440 * 60) - time();
}
?>
setInterval(function() {
var difference = Math.floor('<?php echo $var ;?>');
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
$("#Timer").text(seconds + " Seconds"+ minutes + "m" + hours + "h" + days + "d" );
}, 1000);
</script>
You will be logged out in : <span id="Timer"></span>
Basically the first timer if the dropdown
is 1 should be 6 seconds, which it is, however it is stationary on 6 seconds and doesn't count down at all... I changed this directly from another function that is counting down properly, but I can't seem to find out where I have gone wrong with this one and why won't it actually count down. Basically I want it to go from 6 to 5 to 4 and so on to 0, if it were 6 seconds.
I have jquery/jscript installed as my other timer is working, that does not contain php variables.
Thanks in advance
Upvotes: 0
Views: 131
Reputation: 2117
Place this var difference = Math.floor(<?php echo $var; ?>);
statement outside the anonymous function called by setInterval
. And you don't need '
single quote
You code should be like this:
var difference = Math.floor('<?php echo $var ;?>');
setInterval(function() {
var d = difference; // This line is added
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
difference = d - 1; // This line is added
$("#Timer").text(seconds + " Seconds"+ minutes + "m" + hours + "h" + days + "d" );
}, 1000);
Upvotes: 1