2

如果这个问题有点棘手,我很抱歉,但我没有其他方法可以问它,而且我是 javascript 世界中的绝对菜鸟。

我有这个 javascript 计数器,有人帮助我在 stackoverflow 上收集它,它工作正常,但倒计时计时器将在不同的浏览器上从头开始。

即,当我在 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

?>
4

2 回答 2

3

JavaScript is always running on the client. One instance of the timer has no knowledge of any other instance running elsewhere.

To synchronize time between different browsers, you would need server-side scripts. Using expiration-time-stamps in a server-side script in conjunction with JavaScript timers should give you the synchronization you are looking for.

于 2013-03-26T19:13:11.193 回答
1

Cookie 不会从浏览器传输到浏览器。它们存储在网络浏览器中。

于 2013-03-26T19:14:45.093 回答