创建动态javascript倒计时器

时间:2011-04-11 14:52:37

标签: javascript jquery webmatrix

我正在寻找创建一个动态的javascript倒计时器,我想从我的SQL服务器数据库传递一个日期时间变量并让它倒数到这个日期然后显示一条消息,我已经尝试了几乎每个JQuery插件我可以找到并且无法编辑它们来做我需要的东西,我还需要能够在同一页面上有多个倒计时器,

任何帮助都会非常适合

干杯

斯科特

======= EDIT =======

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

做我需要的事情

2 个答案:

答案 0 :(得分:0)

试试这个:

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

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

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

希望这有帮助:)

答案 1 :(得分:0)

//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