2015-03-02 80 views
1

当时间到了,我需要如何更新MySQL表的帮助?我得到了这样的代码:如何制作倒数计时器并在时间到时更新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表,然后回到同一页。

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

+1

使PHP脚本,将做数据库的东西,然后用你的计数器达到零时调用它调用ajax调用。使用ajax,你也可以根据需要传递一些参数。 – MilanG 2015-03-02 14:57:56

+3

我认为“he.lp”是为了避免错误的标题过滤器?如果是这样,请不要这样做 - 问题不需要寻求帮助,因为它是隐含的(并明确要求它听起来像乞讨,所以它最好避免)。 – halfer 2015-03-02 14:59:42

+0

如果你不熟悉AJAX,这个链接可能会有所帮助:http://www.w3schools.com/ajax/default.asp – BlackCetha 2015-03-02 15:16:40

回答

0

我建议你使用像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

嗨,每一个我发现它谢谢你,这是我的问题的答案 当时间到达0这个页面,然后它返回到相同的页面,并重复,直到没有更多的时间。 COOOL。再次感谢

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