【问题标题】:JavaScript Timer: Uses session start time not workingJavaScript 计时器:使用会话开始时间不起作用
【发布时间】:2014-03-29 17:29:56
【问题描述】:

我正在使用 JavaScript Timer 制作测验系统,该系统使用会话开始时间来计算计时器工作所需的时间。它在某些系统上运行良好,但并非在所有系统上运行良好。
有人可以帮我吗..?

var ct = setInterval("calculate_time()",100); // Start clock.
function calculate_time()
{
   var end_time = "<?php echo $_SESSION['start_time']; ?>"; // Get end time from session variable (total time in seconds).
   var dt = new Date(); // Create date object.
   var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
   var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
   var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
   var secs = total_time - (mins * 60); // Extract remainder seconds if any.

   if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
   document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.

  // Check for end of time, stop clock and display message.
  if(mins <= 0)
  {
     if(secs <= 0 || mins < 0)
     {
        clearInterval(ct);
        document.getElementById("txt").value = "0:00";
        var form = document.createElement("form");
        form.action = "http://www.example.org/q/conflinux/expire_me.php";
        form.method = "post"
        document.body.appendChild(form);
        form.submit();       
     }
   }
}

.....
.....

<form> <input id="txt" readonly> </input></form>

【问题讨论】:

  • 另外,包括不适用的浏览器。
  • 去掉这些“引号” ct = setInterval("calculate_time()",100);
  • 已经在我的答案和他的新代码中
  • 无法在 chrome 33.0.1750.117 m 上使用 win 8 或 win 7

标签: javascript php html session session-cookies


【解决方案1】:

这对我有用 - 我将 ct 设置为全局变量,并且只存储一次结束时间

请将setInterval("calculate_time()",100);改为setInterval(calculate_time,100);

如果不想污染全局范围,可以创建自己的命名空间

Live Demo

var ct, end_time = <?php echo $_SESSION['start_time']; ?>; // Get end time from session variable (total time in seconds). 

window.onload=function() { // make sure the output field exists
  ct = setInterval(calculate_time,100); // Start clock.
}
function calculate_time() {
  var dt = new Date(); // Create date object.
  var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
  var total_time = end_time - Math.round(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
  var mins = Math.floor(total_time / 60); // Extract minutes from seconds remaining.
  var secs = total_time - (mins * 60); // Extract remainder seconds if any.
  if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
  document.getElementById("txt").value = mins + ":" + secs; // Display remaining minutes and seconds.
// Check for end of time, stop clock and display message.
  if(mins <= 0) {
    if(secs <= 0 || mins < 0)  {
     clearInterval(ct);
     document.getElementById("txt").value = "0:00";
     var form = document.createElement("form");
     form.action = "http://www.example.org/q/conflinux/expire_me.php";
     form.method = "post"
     document.body.appendChild(form);
     form.submit();
   }
  }
 }

【讨论】:

  • 先生,它不工作在这里查看...link 使用 ID:326599 电子邮件:shubhamtakode@gmail.com
  • 我在 Chrome 中获得了 25 分钟的倒计时。你哪里有问题?如果是IE,试试按F12看看有没有控制台错误
  • 计时器在 chorme 版本 32.0.1700.107 m 上工作正常,但在 chrome 版本 33.0.1750.117 m 上不显示任何内容或 0:00
  • 我在 Mac 上的 33.0.1750.117 - 我的小提琴也不能使用计数器吗?
  • 先生,我没听懂您的意思吗?我正在使用 win 8。
猜你喜欢
  • 2014-02-13
  • 2014-03-02
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
相关资源
最近更新 更多