I'm using an alternative to javascript alerts in a quiz game I am making. After answering a question, an "alert" (here I am calling swal
, the custom alert) should show and nextQuestion
should be executed after to move to #2 question.
After enabling the custom alert, validation still works fine and the alert/modal shows up fine. However, it moves to the next question before the user clicks it away, unlike how alerts worked. This is a problem as the alert hides the next question (and the timer begins running again).
I have moved that.nextQuestion();
after the validation, but no luck. The quiz moves to nextQuestion
still as soon as the swal
alert appears.
How can I fix this sequence to execute nextQuestion after the alert has been cleared instead of before?
Quiz.prototype.checkAnswer = function(e) {
var selectedIndex = $(e.target).data('index');
var that = this;
$.getJSON('/api/validate_answer/' + this.participationId + '/' + this.quizCurrent, { answer: selectedIndex }, function(data) {
if (data.result) {
swal({ title: "Good job!", text: "You were right!", type: "success", confirmButtonText: "Next" });
that.score += 1;
}
else {
swal({ title: "Wrong!", text: "That wasn't correct...", type: "error", confirmButtonText: "Next" });
}
that.nextQuestion();
});
}
Quiz.prototype.timer = function() {
var that = this;
this.timing--;
$('div.timer strong').text(this.timing);
if (this.timing <= 0) {
$.getJSON('/api/skip_question/' + this.participationId, function() {
that.nextQuestion();
})
}
}