【发布时间】:2015-02-11 13:21:08
【问题描述】:
我尝试计算给定答案的正确和错误。 计数发生在用户单击按钮后的 if else 构造中。
(function keerSom(){
//generate and put a random number between 1 and 10
a = Math.floor((Math.random() * 10) + 1);
$(".a").html(a);
//get the variable b
b = $(".b").text().trim();
//get the cursor inside the input field and set the input field clean
userInputBox.focus();
$("#userAnswer").val("");
//make the calculations and check
next.click(function() {
$("form").submit(function( event ) {
event.preventDefault();
});
result = a*b;
userInput = $("#userAnswer").val();
//check if the answer was correct
if (result == userInput){
userInputBox.removeClass("incorrect");
userInputBox.addClass("correct");
//set count of correct
correct++;
$("#correct").html(correct);
如果答案是正确的,我需要为用户继续下一个任务(这意味着回到函数的开头)
//go for anothe som
setTimeout(function(){
userInputBox.removeClass("correct");
keerSom();
}, 3000);
}else{
userInputBox.addClass("incorrect");
//set counter of incorrect
incorrect++;
$("#incorrect").html(incorrect);
userInputBox.focus();
}
});
})();
你可以在这里看到我想要做什么http://jsfiddle.net/pmjmny49/9/
问题是计数器工作错误。它按级数计算,不是加 1,而是加 2,然后是 4,以此类推。
我无法弄清楚我做错了什么。当然是因为回调函数本身......但我不知道我该怎么做才能让它工作。
如果有人可以提供帮助,请解释发生了什么,我将不胜感激!
提前致谢!
【问题讨论】:
标签: jquery function counter jquery-callback