[英]CountDown timer PHP/JavaScript
嗨,我有一個用於倒計時的JavaScript,它正在選擇時間形式的MySQL並且它正常工作(Chrome,Firefox),但在(IE和Safari)它返回“NaNd NaNh NaNm NaNs”。
我在下面附上了我的代碼。
<?php
$con = mysqli_connect("localhost", "root", "", "timer") or die("Error Could
not connect to the database Sir." . mysqli_error($con));
$query = mysqli_query($con, "SELECT * FROM counter WHERE id = 1") or
die(mysqli_error($con));
$row = mysqli_fetch_array($query)or die(mysqli_error($con));
?>
<div id="form<?php echo $row['id'];?>" style="color:green" class="form-
group">
</div>
<Script>
function createCountDown(elementId, date){
console.log(date);
// Set the date we're counting down to
var countDownDate = new Date(date).getTime();
console.log(countDownDate);
// Update the count down every 1 second
var x = setInterval(function(){
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = (countDownDate) - (now);
//Hint on converting from object to the string.
//var distance = Date.parse(countDownDate) - Date.parse(now);
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
//console.log(days);
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 *
60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById(elementId).innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0)
{
clearInterval(x);
document.getElementById(elementId).innerHTML = "ORDER EXPIRED";
}
}, 1000);
}
createCountDown("form<?php echo $row['id'];?>", "<?php echo
$row['time_to_expire'] ;?>")
</Script>
請檢查我是否遺漏了一些東西。 謝謝你的回復。
仔細檢查'
單引號,使用雙引號作為第二個參數的第一個參數。
createCountDown("form<?php echo $row['id'];?>", "<?php echo $row['time_to_expire'] ;?>")
這是一個有效的例子
function createCountDown(elementId, date) { // Set the date we're counting down to var countDownDate = new Date(date).getTime(); //console.log(countDownDate.getTime()); // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time var now = new Date().getTime(); // Find the distance between now an the count down date var distance = (countDownDate) - (now); //Hint on converting from object to the string. //var distance = Date.parse(countDownDate) - Date.parse(now); // Time calculations for days, hours, minutes and seconds var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Display the result in the element with id="demo" document.getElementById(elementId).innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); document.getElementById(elementId).innerHTML = "ORDER EXPIRED"; } }, 1000); } createCountDown('form1', "08-09-2017 12:10:00Z"); createCountDown('form2', "08-09-2018 12:10:00Z");
<div id="form1"></div> <div id="form2"></div>
編輯
從PHP傳遞到JavaScript的Date()
: 2017-09-30 00:00:00
“格式錯誤” 。 它來自PHP Date("Ymd H:i:s");
這是非常常用的......
JavaScript方面的修正是: date = date.replace(" ","T");
它也可以在PHP端修復: $date = Date("Ymd\\TH:i:s");
或者,如果日期來自數據庫:
$date = str_replace(" ","T",$row['time_to_expire']);
createCountDown("form<?php echo $row['id'];?>", "<?php $date;?>")
生成的日期字符串為2017-09-30T00:00:00
,符合ISO 8601標准。
問題是至少一個角色!
我會記得那一個。 ;)
使用moment.js並嘗試下面的代碼用於計時器
var eventTime = moment("target time").unix(),
currentTime = moment().unix(),
diffTime = eventTime - currentTime,
duration = moment.duration(diffTime * 1000, "milliseconds"),
interval = 1000;
if (diffTime > 0) {
var timer = setInterval(function () {
duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
var h = moment.duration(duration).hours(),
m = moment.duration(duration).minutes(),
s = moment.duration(duration).seconds();
// show how many hours, minutes and seconds are left
var temp = '<div><span>' + h + '</span> hr </div> <div><span>'
+ m + '</span> min </div> <div><span>' + s +
'</span> sec </div>';
$(elementId).html(temp);
if ((h == 0 && m == 0 && s == 0) || (s < 0)) {
clearInterval(timer);
}, interval);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.