倒数计时器从MySQL获得价值

时间:2016-06-17 19:07:44

标签: javascript php mysql

我需要稍微编辑一下我的计时器。

  

编辑 - 添加了php / mysql代码

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT hours, minutes, seconds FROM roll_time WHERE id=1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $hours = $row['hours'];
        $minutes = $row['minutes'];
        $seconds = $row['seconds'];
    }
} else {
    echo "0 results";
}
$conn->close();
?>
<?
date_default_timezone_set('Europe/Prague');

$ahours = date('H');
$aminutes = date('i');
$aseconds = date('s');

$fhours = $hours - $ahours;
$fhours = $fhours * 60 * 60;
$fminutes = $minutes - $aminutes;
$fminutes = $fminutes * 60;
$fseconds = $seconds - $aseconds;


$finalTime = $fhours + $fminutes + $fseconds;
if ($finalTime < 0) {
    $finalTime = 0;
}
?>
    <div>Registration closes in <span id="time">/Loading\</span> seconds!</div>
<br>
<?php echo $ahours.":".$aminutes.":".$aseconds; ?>


<script type="">
function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

window.onload = function () {
    var fiveMinutes = <? echo $finalTime; ?>,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
</script>

价值$ hours,$ minutes,$ seconds将来自MySQL Databse。而我需要的是当计时器到达0:00秒时,然后将使用新值从MySQL和重新启动计时器中获取值。那么,如果你可以帮助我,请?

0 个答案:

没有答案