May Sale! All accounts are 40% off this week.

ricardosoares's avatar

Creating a timer

Hi there, so im using laravel on my project and im creating a timer that i have a start button and a stop button, then when i click on the start button i update the "strat" column on db and on click stop button i update the "stop" column on db and on my php i check the diff between the two times do conver on 00:00:00, but for exemplo if i click "start" i have to manually refresh to se the time running like "00:00:01, 00:00:02" if i dont refresh manually every secound i cant see it running, does anyone know how can i do it work right? i I tried to use js but to refresh the div every secound but i dont know why it's strange and dont refresh every secound, the div refresh but it's slow, im i doing wrong making the time diff on php? i just dont know what to do because i never worked with "real time" things.

If you have another suggestion, or any advice is greatly appreciated!

0 likes
1 reply
ohffs's avatar

In javascript something like this should do a basic timer :

<html>
<body>
<div id="timer">Hello</div>
<button id="start">Start</button>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    function incTimer() {
        var currentMinutes = Math.floor(totalSecs / 60);
        var currentSeconds = totalSecs % 60;
        if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
        if(currentMinutes <= 9) currentMinutes = "0" + currentMinutes;
        totalSecs++;
        $("#timer").text(currentMinutes + ":" + currentSeconds);
        setTimeout('incTimer()', 1000);
    }

    totalSecs = 0;

    $(document).ready(function() {
        $("#start").click(function() {
            incTimer();
        });
    });
</script>
</body>
</html>

Hopefully that'll get you started anyway, if not stopped ;-)

1 like

Please or to participate in this conversation.