2017-02-21 69 views
0

我正在使用兩個函數,一個用於將一組問題移至下一個問題,另一個用於移動進度欄。將用戶移至nextQuestion的問題會覆蓋第一個函數,而progressBar函數不會處理。只有在上一個功能完成時才允許運行Javascript函數

沒有錯誤顯示,我改變了順序或功能,並有一個大功能都有相同的結果。如果我單獨運行這些功能,那麼這兩個功能都可以正常工作它們來自@ onClick按鈕。

我已經嘗試使用承諾與相同的結果。

如何強制nextQuestion函數僅在progressBar完成時才運行?

進度條

 progressBar: function(item){ 

     this.percentQuestion = 100/this.numberQuestions; 

     var elem = document.getElementById("myBar"); 

     var numericWidth = elem.style.width.replace(/\D/g,''); 

     var currentWidth = +numericWidth; 

     var newWidth = +numericWidth + +this.percentQuestion; 

     var id = setInterval(frame, 10); 
     function frame() { 
      if (currentWidth < newWidth) { 
      currentWidth++; 
      elem.style.width = currentWidth + '%'; 
      } else{ 
      clearInterval(id); 
      }; 

      }; 

     this.nextQuestion(item); 

    }, 

下一個問題

 nextQuestion: function(item){ 

     var newKey = item + 1; 

     var y = document.getElementById('question' + item); 

     var x = document.getElementById('question' + newKey); 

      if(y.style.display === 'block'){ 
       y.style.display = 'none'; 
       x.style.display = 'block'; 
       console.log("Changed"); 
     }; 

     }, 

UPDATE

 nextQuestion: function(item){ 


     window.setTimeout(function() { function(item){ 

      var newKey = item + 1; 

      var y = document.getElementById('question' + item); 

      var x = document.getElementById('question' + newKey); 

       if(y.style.display === 'block'){ 
        y.style.display = 'none'; 
        x.style.display = 'block'; 
        console.log("Changed"); 
      }; 
     }, 1000); 

     }, 
+0

正在運行的jsfiddle創造您的方案將是有益的。 – janmvtrinidad

+0

我不完全確定你的意思,但它看起來像你應該把'this.nextQuestion(item)'放在'else','clearInterval(id)'之後。 – Razzildinho

回答

1

執行以下操作做WHA你想要嗎?

progressBar: function(item){ 

    this.percentQuestion = 100/this.numberQuestions; 

    var variables = { 
     elem: document.getElementById("myBar"), 
     numericWidth: elem.style.width.replace(/\D/g,''), 
     currentWidth: +numericWidth, 
     newWidth: +numericWidth + +this.percentQuestion, 
    }; 

    // This isn't the nicest way of doing this but it should work 
    var that = this; 

    var id = setInterval(function(){ 
     frame(item, variables); 
    }, 10); 

    function frame(item, variables) { 
     if (variables.currentWidth < variables.newWidth) { 
     variables.currentWidth++; 
     variables.elem.style.width = variables.currentWidth + '%'; 
     } else{ 
     clearInterval(id); 
     that.nextQuestion(item); 
     }; 

    }; 
}, 
+0

感謝您的這一點...我現在得到'this.nextQuestion不是函數的控制檯錯誤' –

+0

我已經將'this'移動到函數外的變量並將項傳遞到'frame()',但現在得到這個錯誤'不能讀取'null'的屬性'樣式'。 Like item is not reach in'nextQuestion' –

+0

剛剛編輯了答案,在'frame()'函數之外添加了'var that = this'。這有點黑客,但它會希望做你想做的。 – Razzildinho

0

嘗試與添加this.nextQuestion(item);自己的函數中frame()

var self=this; 
function frame() { 
     if (currentWidth < newWidth) { 
     currentWidth++; 
     elem.style.width = currentWidth + '%'; 
     } else{ 
     clearInterval(id); 
     } 
    self.nextQuestion(item); 
    }; 

frame()setInterval()執行你將不得不在每個間隔結束this.nextQuestion(item)

+0

感謝您對此...我現在得到'this.nextQuestion不是函數的控制檯錯誤' –

+0

我正在修改我的答案。請再次檢查, –

+0

請立即看到答案@JamesParsons。這應該現在工作。 –

1

可以使用的setTimeout(HTML API)的setTimeout的方法內

寫nextQuestion功能。

window.setTimeout(function() { 
    function(item){ 
     var newKey = item + 1; 

     var y = document.getElementById('question' + item); 

     var x = document.getElementById('question' + newKey); 

     if(y.style.display === 'block'){ 
      y.style.display = 'none'; 
      x.style.display = 'block'; 
      console.log("Changed"); 
     }; 
    }, 1000); 
+0

我能否知道是誰和爲什麼有人已經對此答案投了贊成票? – Jerome

+0

我已經更新了我的問題,目前我的語法錯誤。抱歉,不習慣使用超時。 @Jerome –

+0

@JamesParsons對我來說你不應該使用超時!這就是爲什麼我想知道爲什麼有人upvoted這個答案,也許他可以解釋我爲什麼 – Jerome

0

這些信息存儲在一個變量第一則只是移動 this.nextQuestion(item);if (currentWidth < newWidth) { currentWidth++; elem.style.width = currentWidth + '%'; } else{ that.nextQuestion(item); clearInterval(id); };

+0

我現在得到一個控制檯錯誤'this.nextQuestion不是一個函數' –

+0

對不起,這種情況已經改變。只需將其存儲在一個變量中。 –

+0

我們幾乎在那裏添加了'函數框架(item)',但仍然得到'無法讀取null'的屬性'style',就像'item'一樣沒有通過。 –

相關問題