如何制作倒计时计时器并在时间到时更新MySQL记录

How to Make A Countdown Timer And Update MySQL Record When The Time is Up?

本文关键字:更新 MySQL 记录 时间 何制作 倒计时 计时器      更新时间:2023-09-26

我需要关于如何在时间到时更新MySQL表的帮助?我得到了这个代码:

<script language="JavaScript">
var timeleft;
var nextdue;
var tick = 1000;
function parseTime(t) {
    var tt = ("0:00:" + t);
    tt = tt.split(":").reverse();
    return (tt[0] * 1000) + (tt[1] * 60000) + (tt[2] * 3600000);
}
function zeroPad(n) {
    if (n < 10) return "0" + n;
    return "" + n;
}

function makeTime(t) {
    if (t < 0) return "0:00:00";
    var tt = t + 999;
    return Math.floor(tt / 3600000) + ":" +
        zeroPad(Math.floor(tt / 60000) % 60) + ":" +
        zeroPad(Math.floor(tt / 1000) % 60);
}
function startTimer() {
    nextdue = new Date().getTime();
    timeleft = parseTime(document.timerform.timerbox.value);
    runTimer();
}
function runTimer() {
    document.timerform.timerbox.value = makeTime(timeleft);
    if (timeleft <= 0) alert("Time's up!");
    else {
        var timecorr = (new Date().getTime()) - nextdue;
        if (timecorr > 0 && timecorr < 3000) {
            timeleft -= (tick + timecorr);
            nextdue += tick;
            if (timeleft < 1) setTimeout("runTimer()", tick + timeleft);
            else setTimeout("runTimer()", Math.max(1, tick - timecorr));
        }
        else {
            nextdue = (new Date().getTime()) + tick;
            timeleft -= tick;
            if (timeleft < 1) setTimeout("runTimer()", tick + timeleft);
            else setTimeout("runTimer()", tick);
        }
    }
}
</script>

而且,当我放置一个PHP变量来加载时间时,它就起作用了;但是,我想像下面这样使用它:

  • 当它达到0秒时,我希望它更新mySQL表,然后返回到同一页

这可能吗?非常感谢。这是我第一次来这里。

我建议您使用像jQuery这样的库,或者像Backbone.js这样的框架,让您的生活在处理javascript任务时更轻松一些。

要想做你想做的事(如果我理解正确的话),你需要以下内容。在下面的例子中,我假设您的网页中包含jquery。

/* 
 * Your time-tracking code here 
*/
// If the time left is 0 seconds/milliseconds/whatever
if(time_left === 0) {
    // then do an AJAX request to the server page that handles updating the mysql table
    $.ajax({
        type: operation, // (GET, POSt, etc)
        url: url, // The location of your php file relative to your root URL
        data: {}, // the data you want the server (php file) to receive
        success: function(data){
            // Whatever you want to happen when the server succeeds with the operation
        }
    });
}

在php文件中,您只需拥有一个函数即可根据需要更新表。

大家好,我找到了,谢谢,这是我问题的答案当时间到达0到达该页面时,它返回到同一页面并重复,直到不再有时间为止。coool。再次感谢

if (timeleft<=0)window.location.href="./mod.php";