1. 程式人生 > >定時請求php 後臺 AJAX 請求,在頁面中倒計時,和顯示重新整理時間

定時請求php 後臺 AJAX 請求,在頁面中倒計時,和顯示重新整理時間

這個是通過ajax , 普通的重新整理,在另一篇文章

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定時器</title>
</head>
<body>

<div style="text-align: center;margin-top: 20%">
    <div>距離重新整理還有<span id="mes"style="color: red">5</span></div>
   <span id="time"></span>--重新整理成功
</div>
</body>
<script src="__PUBLIC__/Admin/js/jquery.min.js"></script>
<script>

    function dataajax() {
        $.ajax({url:"./auto",success:function(result){
        }});
    }
    var time;
    time = getNowFormatDate();

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        return currentdate;
    }



    //還有幾秒;
    $(function () {
        funkaishi();
        $("#time").html(time);
    })
    var i = 4;
    var intervalid;
    function funkaishi() {
        intervalid = setInterval("fun()", 1000);
    }
    function fun() {
        var  time2 = getNowFormatDate();

        if (i == 0) {
            dataajax();
            clearInterval(intervalid);
            i = 5;
            funkaishi();
            $("#time").html(time2);
        }
        document.getElementById("mes").innerHTML = i;
        i--;
    }

</script>
</html>