创建一个动态javascript倒计时计时器

Creating a dynamic javascript countdown timer

本文关键字:动态 javascript 倒计时 计时器 一个 创建      更新时间:2023-09-26

我想创建一个动态的javascript倒计时计时器,我想通过它从我的SQL服务器数据库的datetime变量,让它计数到这个日期然后显示一条消息,我已经尝试了几乎每一个JQuery插件我能找到,还没有能够编辑他们做我需要的,我还需要能够有多个倒计时计时器在同一页面,

任何帮助都将是非常感激的

欢呼斯科特

= = = = = = = = = = = = = =编辑

经过多次尝试和错误,我能够修改这个js http://andrewu.co.uk/clj/countdown/pro/

try this:

function Thick(startin) {
  startin--;
  document.getElementById('timer').innerHTML = startin;
  if(startin > 0) setTimeout('Thick(' + startin + ')', 1000);
}

在body onLoad中调用这样的函数:

<body onLoad="Thick(20);">

希望有帮助

//TODAY'S DATE
$today = time();
//FETCHES DATE AND TIME FOR THE EVENT FROM DATABASE
$sql = "SELECT * FROM post";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$Row = (mysqli_fetch_assoc($result));
$th = $Row['endtime'];    
}
echo $th
first of all put it into a variable then use your javascript to call the variable
//let get todays date here
var today = new Date();
var DD = today.getDate();
var MM = today.getMonth()+1; //January is 0!
var YYYY = today.getFullYear();
//let get the Difference in Sec btw the two dates
var _DateFromDBProgEndDate = '<?php echo $th; ?>';
var ProgEndTime = new Date(_DateFromDBProgEndDate);
var TodayTime = new Date();
var differenceTravel = ProgEndTime.getTime()- TodayTime.getTime() ;
var seconds = Math.floor((differenceTravel) / (1000));
////////////////////////////////
var SecDiffFromToday = seconds;
var seconds = SecDiffFromToday;
function timer() {
    var days        = Math.floor(seconds/24/60/60);
    var hoursLeft   = Math.floor((seconds) - (days*86400));
    var hours       = Math.floor(hoursLeft/3600);
    var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
    var minutes     = Math.floor(minutesLeft/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds; 
    }
    document.getElementById('countdown').innerHTML = days + ":" + hours + ":" + minutes + ":" + remainingSeconds;
    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "Completed";
    } else {
        seconds--;
    }
}
var countdownTimer = setInterval('timer()', 1000);
</script>

the  javascript call in the variable with  '<?php echo $th; ?>'; then the javascript does the count down with out refreshing the page