gpt4 book ai didi

javascript倒计时在不同浏览器上同步

转载 作者:行者123 更新时间:2023-11-30 10:33:10 25 4
gpt4 key购买 nike

如果这个问题有点棘手,我很抱歉,但我没有其他方法可以问这个问题,而且我绝对是 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>

您可以在此处的 fiddle 上查看它: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

?>

最佳答案

JavaScript 始终在客户端上运行。计时器的一个实例不知道在别处运行的任何其他实例。

要在不同浏览器之间同步时间,您需要服务器端脚本。在服务器端脚本中结合使用过期时间戳和 JavaScript 计时器应该可以实现您正在寻找的同步。

关于javascript倒计时在不同浏览器上同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com