JSfiddle(显示非工作代码):http://jsfiddle.net/ts2Rr/
我有三个框,用户必须一次单击一个框以查看该框的内容。在看到该框的内容后,用户将文本输入到文本框中,然后显示下一个框。但是,用户只有五秒钟的时间。到目前为止测试代码似乎表明失败发生在if else语句周围,但我无法弄清楚是什么问题。如果代码中没有if else语句,那么一切都可以解决。使用代码中的if else语句没有任何作用(甚至不是之前的代码)。
使用if else语句我试图使用两个变量(一个在函数开始时设置,另一个在用户输入文本时设置)。然后我减去这两个以得到一个毫秒时间变量。如果小于5,000毫秒,则将其记录到隐藏的表单字段中。否则,运行一个setTimeOut函数,记录隐藏表单字段5秒。这甚至可能吗?
//start after 3 seconds on page load
//settimeout()
//appends onclick onto #Box 1 that calls function Rate when clicked
//.toggleclass onto #Box1
//300;
window.onload = function(){
setTimeout(function(){
$('#BoxE1').bind('click', function() {
Rate();
//alert('User clicked on "foo."'); //uncomment for testing purposes only
});
$('#BoxE1').toggleClass('inactive active');
}, 3000);
};
function Rate() {
$('#E1Rate').show(); //Show the object to be rated
$('#BoxE1').unbind('click'); //Prevent the user from clicking on the box again
$('#BoxE1').toggleClass('inactive active'); //Toggles the box back to green
//Code works up to this point so far, if I need to rollback just add }; to the next line
var startTime = new Date();
var elaspedTime = (inputTime - startTime); //Calculate the elasped milliseconds
if (elaspedTime < 5000) {
document.getElementById("E1RatingTime").value=elaspedTime;
}
else {
setTimeout (function(){
document.getElementById("E1RatingTime").value="5";
}, 5000);
}
$('#E1Rate').hide();
};
答案 0 :(得分:3)
由于
中的语法错误,您的代码停止工作SetTimeout function(){
纠正
setTimeout(function() {...}, 5000);
顺便说一下。 getElementById()
在id字符串之前不需要#hash。它甚至不适用于#