Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript和PHP的24小时倒计时_Javascript_Jquery_Html_Wordpress - Fatal编程技术网

使用Javascript和PHP的24小时倒计时

使用Javascript和PHP的24小时倒计时,javascript,jquery,html,wordpress,Javascript,Jquery,Html,Wordpress,我需要一个24小时倒计时像在链接中的图像。我需要在我的wordpress站点上实现计时器。计时器应在美国东部时间每晚午夜重置 这是我当前的JS代码,但每次刷新页面时它都会重新启动。我能把它和EST整合起来吗 <script type = "text/javascript"> var timeInSecs; var ticker; function startTimer(secs) { timeInSecs = parseInt(secs); ticker = setInt

我需要一个24小时倒计时像在链接中的图像。我需要在我的wordpress站点上实现计时器。计时器应在美国东部时间每晚午夜重置

这是我当前的JS代码,但每次刷新页面时它都会重新启动。我能把它和EST整合起来吗

    <script type = "text/javascript">

var timeInSecs;
var ticker;

function startTimer(secs) {
timeInSecs = parseInt(secs);
ticker = setInterval("tick()", 1000); 
}

function tick( ) {
var secs = timeInSecs;
if (secs > 0) {
timeInSecs--; 
}
else {
clearInterval(ticker);
startTimer(172800);  // start again
}

var hours= Math.floor(secs/3600);
secs %= 3600;
var mins = Math.floor(secs/60);
secs %= 60;
var pretty = ( (hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins + ":" + ( (secs < 10) ? "0" : "" ) + secs;
document.getElementById("countdown").innerHTML = pretty;
}

startTimer(86400);  // 24 hours in seconds

</script>

<span id="countdown" style="font-weight: bold;"></span>

时间昆虫;
var-ticker;
功能启动计时器(秒){
timeInSecs=parseInt(秒);
ticker=setInterval(“tick()”,1000);
}
函数勾号(){
var secs=时间单位;
如果(秒>0){
时光虫;
}
否则{
清除间隔(股票代码);
startTimer(172800);//重新开始
}
var小时=数学楼层(秒/3600);
秒%=3600;
var mins=数学层(秒/60);
secs%=60;
变量pretty=((小时<10)?“0”:“)+小时+”:“+((分钟<10)?“0”:“)+分钟+”:“+((秒<10)?“0”:“)+秒;
document.getElementById(“倒计时”).innerHTML=pretty;
}
startTimer(86400);//24小时每秒

我在家的时候会再看一看,但是您的问题是每次加载页面时都在调用startTimer()方法,您需要做的是获取当前系统时间(EST格式)并将其转换为秒
这样,当您的页面刷新时,您将拥有当前时间,而不是当前定义的常量-我希望这是您找到解决方案的基础

我得到了答案。正如Alex所说,我需要获取当前系统时间(EST)。我提出了index.html和server_time.php文件

INDEX.HTML

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Countdown</title>
<script src="http://code.jquery.com/jquery-2.1.0.js"></script> 
<script>
    var root_path = "http://localhost/countdown/";
    function init(){
        $.ajax({
            type: "POST",
            url: root_path + "server_time.php",
            beforeSend: function(aj_msg) {
                //$("#countdown").html('');
            },
            success: function(aj_msg){
                $("#countdown").html('');
                phpMsg = JSON.parse(aj_msg);
                var est = "<span>" + Math.abs(phpMsg['ServerTime'][0].hour) + " :</span> ";
                est += Math.abs(phpMsg['ServerTime'][0].minute) + " : " ;
                est += Math.abs(phpMsg['ServerTime'][0].second);
                $("#countdown").html(est);
            }
        })
    }

    setInterval(init, 1000)
</script>
</head>

<body>
    <div id="countdown"></div>
</body>
</html>

倒计时
变量根路径=”http://localhost/countdown/";
函数init(){
$.ajax({
类型:“POST”,
url:root\u path+“server\u time.php”,
发送前:函数(aj_msg){
//$(“#倒计时”).html(“”);
},
成功:功能(aj_msg){
$(“#倒计时”).html(“”);
phpMsg=JSON.parse(aj_msg);
var est=“”+Math.abs(phpMsg['ServerTime'][0].hour)+“:”;
est+=Math.abs(phpMsg['ServerTime'][0].minute)+“:”;
est+=Math.abs(phpMsg['ServerTime'][0]。秒);
$(#倒计时”).html(美国东部标准时间);
}
})
}
设置间隔(初始值,1000)
和server_time.php

<?php
    date_default_timezone_set('US/Eastern');
    $currenttime = date('G:i:s:u');
    list($hrs,$mins,$secs,$msecs) = split(':',$currenttime);
    $output = '{"ServerTime": [';
    $hrs -= 24;
    $mins -= 60;
    $secs -= 60;
    $output .= '{ "hour":"' . $hrs . '" , "minute":"' . $mins . '", "second":"' . $secs . '"}'; 
    $output .=']}';

    echo $output;
?>


欢迎来到StackOverflow。如果你展示了你在研究和编码方面的尝试,并解释了你在代码方面遇到的特定问题,那么这个网站上的问题最有效。相反,这个问题要求为您免费编写代码,或者提供指向现有代码的链接列表,您可能会在搜索引擎中找到这些代码。很抱歉,我不熟悉这一点。请您演示如何定义该值,并将其插入“time”变量?我可以从某处获得EST的值而不是本地系统时间吗?