保持执行功能直到自定义javascript警报清除后

时间:2016-01-23 06:10:16

标签: javascript jquery

我在我正在制作的测验游戏中使用javascript警报的替代方法。在回答问题后,提醒"警告" (此处我调用swal,自定义提醒)应该显示,并且在移至#2问题后应执行nextQuestion

启用自定义提醒后,验证仍然正常,警报/模式显示正常。但是,它会移动到用户点击它之前的之前的下一个问题,与警报的工作方式不同。这是一个问题,因为警报隐藏了下一个问题(并且计时器再次开始运行)。

验证后我已移动that.nextQuestion();,但没有运气。只要nextQuestion警报出现,测验就会移至swal

如何在清除警报后而不是之前修复此序列以执行nextQuestion?

 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();
   })
  }
 }

1 个答案:

答案 0 :(得分:1)

您应该将confirm移动到swal({ title: "Wrong!", text: "That wasn't correct...", type: "error", confirmButtonText: "Next" }, function() { that.nextQuestion(); }); 按钮附带的功能中。例如。如下错误答案。

检查演示:reset_index

{{1}}