對不起,如果這個問題有點棘手,但我沒有其他的方式來問它,我是一個JavaScript世界絕對noob。在不同瀏覽器上同步的javascript倒計時
我有這個javascript計數器,有人幫助我收集在計算器上,它工作正常,但 倒計時器將從一開始在不同的瀏覽器上開始。
即當我在Firefox上查看它時,它將繼續進行倒計時,並且它將保持正常工作。可以說定時器設置爲倒計時一天。在Firefox上它將顯示23:24小時/分鐘,但是如果我打開一個新的瀏覽器(任何瀏覽器),它將顯示計時器23:59小時/分鐘,並且將從那裏開始倒計時......即使倒計時器已經在同一頁上運行!
這裏是代碼:
<script type="text/javascript">
var EXPIRY = parseInt(new Date().getTime()/1000) + 24*60*60;
var counter = null;
var counter_interval = null;
function setCookie(name,value,days) {
console.log("setting "+name+" "+value);
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
function deleteCookie(name) {
setCookie(name,"",-1);
}
function resetCounter() {
EXPIRY = parseInt(new Date().getTime()/1000) + 24*60*60;
}
function stopCounter() {
window.clearInterval(counter_interval);
deleteCookie('Expiry');
}
function updateCounter() {
var msg = '';
curTime = parseInt(new Date().getTime()/1000);
if (curTime < EXPIRY) {
msg = convertSecondsToDays(EXPIRY - curTime);
}
else {
EXPIRY = parseInt(new Date().getTime()/1000) + 24*60*60;
}
var el = document.getElementById('counter');
if (el) {
el.innerHTML = msg
}
}
function convertSecondsToDays(sec) {
var days, hours,rem,mins,secs;
days = parseInt(sec/(24*3600));
rem = sec - days*3600
hours = parseInt(rem/3600);
rem = rem - hours*3600;
mins = parseInt(rem/60);
secs = rem - mins*60;
return days +":" + hours +":"+mins + ":"+ secs + "";
}
function startCounter() {
stopCounter();
setCookie('Expiry', EXPIRY, 1);
counter_interval = window.setInterval(updateCounter, 1000);
}
function init() {
EXPIRY = getCookie('Expiry');
if (!EXPIRY) {
console.log("unable to find cookie");
resetCounter();
}
startCounter();
}
init();
</script>
,您可以查看它的小提琴這裏:http://jsfiddle.net/h2DEr/1/
我如何使它的方式,它會顯示留在所有的瀏覽器,同時因爲它會顯示在同一頁面上?
感謝
編輯:
我發現這個代碼與MySQL的作品。它工作正常,但不是倒計時它會顯示多少天/小時/分鐘的產品/項目在網站上發佈。這並不需要任何JavaScript作爲是...
這有點什麼,我尋找,但不是計數它需要倒計時:
<?php
//list fields and convert to seconds
$countdown['days']=(5) * 24 * 60 * 60;
$countdown['hours']=(3) * 60 * 60;
// etc, etc
$countsum=time() + $countdown['days'] + $countdown['hours']; //and so on
// the above would be the timestamp to enter into the table
##########
// 'dumbed down' query
include "config/connect_to_mysql.php";
$result=mysql_query("SELECT * FROM tomProduct WHERE id='id';");
while ($row=mysql_fetch_assoc($result))
$time=$row['date_added'] - time(); //this field would be a PHP timestamp (time())
$count=getdate($time);
$x=getdate(); //todays information
$count['mday'] -= $x['mday'];
$count['hour'] -= $x['mday'];
$count['minutes'] -= $x['minutes'];
echo "$count[mday] Days $count[hour] Hours $count[minutes] Minutes"; //etc
// untested, but should work
?>
這是什麼無國籍的代碼呢? – adeneo 2013-03-26 19:16:57
@adeneo,這正是代碼應該做的。 – 2013-03-26 19:25:46