我正在使用兩個函數,一個用於將一組問題移至下一個問題,另一個用於移動進度欄。將用戶移至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);
},
正在運行的jsfiddle創造您的方案將是有益的。 – janmvtrinidad
我不完全確定你的意思,但它看起來像你應該把'this.nextQuestion(item)'放在'else','clearInterval(id)'之後。 – Razzildinho