Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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/1/php/231.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变量的JS倒计时(AJAX)_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 来自PHP变量的JS倒计时(AJAX)

Javascript 来自PHP变量的JS倒计时(AJAX),javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,下面的脚本以分钟和秒为单位返回自上次客户注册以来的时间。注册日期变量从单独的PHP文件中提取 如何在新客户注册时将两者结合起来以获得动态结果(AJAX) 下面的脚本运行良好,但仅在重新加载时更新 index.php <?php require_once('get_file.php') ?> <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> <script

下面的脚本以分钟和秒为单位返回自上次客户注册以来的时间。注册日期变量从单独的PHP文件中提取

如何在新客户注册时将两者结合起来以获得动态结果(AJAX)

下面的脚本运行良好,但仅在重新加载时更新

index.php

<?php
    require_once('get_file.php')
?>

<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

<script type="text/javascript">
var currenttime = <?php print json_encode($todaysDate, JSON_UNESCAPED_SLASHES) ?>;
var lastsale = <?php print json_encode($orderCreated, JSON_UNESCAPED_SLASHES) ?>;

var montharray=new Array("01","02","03","04","05","06","07","08","09","10","11","12")
var serverdate=new Date(currenttime)
var lastordercreated=new Date(lastsale)

function padlength(what){
    var output=(what.toString().length==1)? "0"+what : what
    return output
}

function displaytime(){
    serverdate.setSeconds(serverdate.getSeconds()+1)

    // Todays Date
    var datestring=padlength(serverdate.getDate())+"/"+montharray[serverdate.getMonth()]+"/"+serverdate.getFullYear()

    // Current Time
    var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())

    // Last Ordered Count
    var lastorderedcount=padlength(serverdate.getTime()) - padlength(lastordercreated.getTime())

    var minutes = Math.floor((lastorderedcount % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((lastorderedcount % (1000 * 60)) / 1000);
    var lastordercountdown = minutes+":"+seconds

    // Output
    document.getElementById("todaysdate").innerHTML=datestring
    document.getElementById("currenttime").innerHTML=timestring
    document.getElementById("lastordered").innerHTML=lastordercountdown

}   

function updateAjax() {
    $.ajax({
        url: 'getLastData.php',
        success: function(res) {
            currenttime         = res.currentDate;
            lastsale            = res.orderCreated;
            serverdate          = new Date(currenttime);
            lastordercreated    = new Date(lastsale);
            console.log(res);
        }
    });
}

window.onload=function(){
setInterval('displaytime()', 1000)
setInterval(updateAjax, 1000)
}
</script>

<span id="lastordercountdown"></span>
<?php
    require_once('get_data.php');

    print json_encode([
        'currentDate' => $todaysDate,
        'orderCreated' => $orderCreated
    ],JSON_UNESCAPED_SLASHES);
?>

var currenttime=;
var lastsale=;
var montharray=新数组(“01”、“02”、“03”、“04”、“05”、“06”、“07”、“08”、“09”、“10”、“11”、“12”)
var serverdate=新日期(当前时间)
var lastordercreated=新日期(lastsale)
功能焊盘长度(what){
变量输出=(what.toString().length==1)?“0”+what:what
返回输出
}
函数displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
//今天的日期
var datestring=padlength(serverdate.getDate())+“/”+montharray[serverdate.getMonth()]+“/”+serverdate.getFullYear()
//当前时间
var timestring=padlength(serverdate.getHours())+“:”+padlength(serverdate.getMinutes())+“:”+padlength(serverdate.getSeconds())
//最后订购数量
var lastorderedcount=padlength(serverdate.getTime())-padlength(lastordercreated.getTime())
var minutes=数学楼层((lastorderedcount%(1000*60*60))/(1000*60));
var秒数=数学楼层((lastorderedcount%(1000*60))/1000);
var lastordercountdown=分钟+“:”+秒
//输出
document.getElementById(“todaysdate”).innerHTML=datestring
document.getElementById(“currenttime”).innerHTML=timestring
document.getElementById(“lastordered”).innerHTML=lastordercountdown
}   
函数updateAjax(){
$.ajax({
url:'getLastData.php',
成功:功能(res){
currenttime=res.currentDate;
lastsale=res.orderCreated;
serverdate=新日期(当前时间);
lastordercreated=新日期(lastsale);
控制台日志(res);
}
});
}
window.onload=function(){
setInterval('displaytime()',1000)
setInterval(updateAjax,1000)
}
getLastData.php

<?php
    require_once('get_file.php')
?>

<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

<script type="text/javascript">
var currenttime = <?php print json_encode($todaysDate, JSON_UNESCAPED_SLASHES) ?>;
var lastsale = <?php print json_encode($orderCreated, JSON_UNESCAPED_SLASHES) ?>;

var montharray=new Array("01","02","03","04","05","06","07","08","09","10","11","12")
var serverdate=new Date(currenttime)
var lastordercreated=new Date(lastsale)

function padlength(what){
    var output=(what.toString().length==1)? "0"+what : what
    return output
}

function displaytime(){
    serverdate.setSeconds(serverdate.getSeconds()+1)

    // Todays Date
    var datestring=padlength(serverdate.getDate())+"/"+montharray[serverdate.getMonth()]+"/"+serverdate.getFullYear()

    // Current Time
    var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())

    // Last Ordered Count
    var lastorderedcount=padlength(serverdate.getTime()) - padlength(lastordercreated.getTime())

    var minutes = Math.floor((lastorderedcount % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((lastorderedcount % (1000 * 60)) / 1000);
    var lastordercountdown = minutes+":"+seconds

    // Output
    document.getElementById("todaysdate").innerHTML=datestring
    document.getElementById("currenttime").innerHTML=timestring
    document.getElementById("lastordered").innerHTML=lastordercountdown

}   

function updateAjax() {
    $.ajax({
        url: 'getLastData.php',
        success: function(res) {
            currenttime         = res.currentDate;
            lastsale            = res.orderCreated;
            serverdate          = new Date(currenttime);
            lastordercreated    = new Date(lastsale);
            console.log(res);
        }
    });
}

window.onload=function(){
setInterval('displaytime()', 1000)
setInterval(updateAjax, 1000)
}
</script>

<span id="lastordercountdown"></span>
<?php
    require_once('get_data.php');

    print json_encode([
        'currentDate' => $todaysDate,
        'orderCreated' => $orderCreated
    ],JSON_UNESCAPED_SLASHES);
?>

使用jQuery,您可以执行以下操作:

getLastCustomer.php:

<?php
    require_once('get_file.php')
?>

echo json_encode(['currentDate' => $todaysDate, 'lastRegistered' => $lastRegistered]);

echo json_encode(['currentDate'=>$todaysDate,'lastRegistered'=>$lastRegistered]);
您的主要脚本:

<script type="text/javascript">

var currenttime = '<?php print (json_encode($todaysDate)) ?>'
var lastregistered = <?php print (json_encode($lastRegistered)) ?>

var montharray=new Array("01","02","03","04","05","06","07","08","09","10","11","12")
var serverdate=new Date(currenttime)
var registrationdate=new Date(lastregistered)

function padlength(what){
    var output=(what.toString().length==1)? "0"+what : what
    return output
}

function displaytime(){
    serverdate.setSeconds(serverdate.getSeconds()+1)


    // Last Registration Countdown
    var lastregcount=padlength(serverdate.getTime()) - padlength(registrationdate.getTime())

    var minutes = Math.floor((lastregcount % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((lastregcount % (1000 * 60)) / 1000);
    var lastregcountdown = minutes+":"+seconds

    document.getElementById("registrationcount").innerHTML=lastregcountdown

}   

function updateAjax() {
    $.ajax({
        url: 'getLastCustomer.php',
        dataType: 'json',
        success: function(res) {
            currenttime      = res.currentDate;
            lastregistered   = res.lastRegistered;
            serverdate       = new Date(currenttime);
            registrationdate = new Date(lastregistered)
        }
    });
}        

window.onload=function(){
    setInterval(displaytime, 1000)
    setInterval(updateAjax, 30 * 1000)
}
</script>

var currenttime=“”
var lastregistered=
var montharray=新数组(“01”、“02”、“03”、“04”、“05”、“06”、“07”、“08”、“09”、“10”、“11”、“12”)
var serverdate=新日期(当前时间)
var注册日期=新日期(lastregistered)
功能焊盘长度(what){
变量输出=(what.toString().length==1)?“0”+what:what
返回输出
}
函数displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
//最后登记倒数
var lastregcount=padlength(serverdate.getTime())-padlength(registrationdate.getTime())
var minutes=数学楼层((lastregcount%(1000*60*60))/(1000*60));
var秒数=数学地板((lastregcount%(1000*60))/1000);
var lastregcountdown=分钟+“:”+秒
document.getElementById(“registrationcount”).innerHTML=lastregcountdown
}   
函数updateAjax(){
$.ajax({
url:'getLastCustomer.php',
数据类型:“json”,
成功:功能(res){
currenttime=res.currentDate;
lastregistered=res.lastregistered;
serverdate=新日期(当前时间);
注册日期=新日期(上次注册)
}
});
}        
window.onload=function(){
设置间隔(显示时间,1000)
setInterval(updateAjax,30*1000)
}

您是否在询问如何执行原始ajax请求?现在,我看不到任何AJAX。我们是在没有任何库的帮助下这样做的,还是可以使用jQuery?这是怎么回事,甚至是怎么回事?你用PHP打印出一个整数作为时间,然后用javascript每秒递增一个日期格式。jQuery是拉式的,但我没有使用AJAX的经验。只要id元素得到动态更新,就可以包含任何库。理想情况下,对PHP的请求将在JSON更改后进行。如果我得到无效的格式日期为:NaN/undefined/NaNAdd some
console.log
,请查看从服务器返回的内容。是否设置了正确的日期时间?JSON的输出是:{“currentDate”:“03/03/2017 13:01:33”}{“lastRegistered”:“03/03/2017 13:00:58”},并且该文件可以很好地重新加载。$.ajax中缺少的部分是:数据类型:“JSON”,更新为@damek132