Reputation: 5361
Okay so I'm asking questions in a pop quiz on my web page. The questions are randomly generated in a loop that kind of looks like this:
for(var ii=1;ii<=numQuestions;ii++){
var tempScore = askQuestion(difficulty)
}
askQuestion() basically creates a question and 4 possible answers (1 is correct) and puts it on the web page. The only thing is, it does all the iterations before I can even answer one. Is there some way I can wait until some class is clicked on before going to the next iteration?
EDIT: I guess a better way to state this question is, how can I make the function askQuestion() only finish and return when a button is clicked on?
Upvotes: 0
Views: 61
Reputation: 5340
var numQuestions = 10;
askQuestion(difficulty);
$('button').click(function() {
numQuestions--;
if (numQuestions > 0) {
askQuestion(difficulty);
} else {
//all questions have been answered
}
});
Upvotes: 1
Reputation: 11353
You can use a simple recursive approach
function nextQuestion(i) {
var tempScore = askQuestion(difficulty);
if(i < numQuestions) {//do processing/wait for event
document.querySelector(".xxx").addEventListener("click", function() {
nextQuestion(i+1);
});
}
}
Upvotes: 1