我是JS的新手,我们创建了一个时钟代码,从重新加载同一页面或移动到下一页的结束时间开始但是这不能正常工作。我在这里显示javascript代码。我们将此添加到limesurvey js文件中。
Js代码:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
function startTimer(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
// get the number of seconds that have elapsed since
diff = duration - (((Date.now() - start) / 1000) | 0);
// does the same job as parseInt truncates the float
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds ;
if (diff <= 0) {
display.textContent ="Time finished ";
alert("Time is over.Click ok to submit Exam.")
$("#movesubmitbtn").click();
clearInterval(handle);
}
}
// we don't want to wait a full second before the timer starts
timer();
var handle = setInterval(timer, 1000);
}
window.onload = function(){
//set cookie if not exist
if(document.cookie && document.cookie.match('examDuration')){
examTime = document.cookie.match(/(^|;)examDuration=([^;]+)/)[2];
}
else{
timeLimit = getURLParameter('examTime');
document.cookie = 'examDuration='+ 60*timeLimit + '; path=/; ' ;
examTime= 60*timeLimit ;
}
var display = document.querySelector('#time');
startTimer(examTime, display);
$("#movenextbtn").onclick(function(){
remainingTime = (minutes * 60) + seconds ;
document.cookie = 'examDuration='+ remainingTime + '; path=/; ' ;
clearInterval(handle);
});
$("#movesubmitbtn").onclick(function(){
remainingTime = (minutes * 60) + seconds ;
document.cookie = 'examDuration='+ remainingTime + '; path=/; ' ;
clearInterval(handle);
});
};
我不知道为什么它不起作用。我已经在头文件中包含了JS库链接,并且已经采取了所有必要的步骤。提前致谢。