2014-02-26 32 views
-1

我正在使用JavaScript Timer来制作测验系统,它使用会话开始时间来计算定时器工作所需的时间。它在一些系统上工作正常,但不是全部。
可以请一些帮助我..?JavaScript定时器:使用会话开始时间不工作

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> 
+0

此外,包括这是不工作的浏览器。 –

+0

删除这些“引号” ct = setInterval(“calculate_time()”,100); –

+0

已经在我的回答和他的新代码 – mplungjan

回答

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(); 
    } 
    } 
} 
+0

先生它不工作检查它在这里... [链接](http://2014.pragyaa.org/ q/conflinux /)使用ID:326599电子邮件:[email protected] –

+0

究竟哪些不起作用?时钟或提交或什么? – mplungjan

+0

计时器时钟 –