0

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

3
  • 2
    Please post the JavaScript code as it is received by the browser (view source code for the URL). Commented Mar 28, 2014 at 16:43
  • @AbhiBeckert I'm not following I rightclicked the page and hit view source code, and it's exactly the same as what is posted there above Commented Mar 28, 2014 at 16:46
  • That explains it then! Commented Mar 28, 2014 at 16:49

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Progress, however it jumps from 6 straight to 0
@Jim, I added 2 line the the code, please check again
@Jim, I think you might want to stop the timer when count down becomes zero. Or you might want to redirect to login page. Hope you can do it! Otherwise ask new question.
is there a way to auto redirect, i do have a "header(Location : login.php)" however I have to refresh this to take me back, it doesn't auto redirect if you know what I mean, when the timer hits 0 it calls this, however i need to refresh before the page gets loaded
@Jim, so, post a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.