仅允许在前一个函数完成时运行Javascript函数
我使用两个功能,一个是将一组问题移动到下一个问题,另一个是移动进度条。将用户移动到仅允许在前一个函数完成时运行Javascript函数,javascript,function,Javascript,Function,我使用两个功能,一个是将一组问题移动到下一个问题,另一个是移动进度条。将用户移动到nextQuestion的问题会覆盖第一个函数,而progressBar函数不会处理 没有显示任何错误,我已经改变了顺序或函数,拥有一个大函数都会得到相同的结果。如果我分别运行这两个函数,这两个函数都能完美工作。它们起源于@onClick上的按钮 我尝试过使用承诺,但效果相同 如何仅在progressBar完成时强制运行nextQuestion功能 进度条 progressBar: function(i
nextQuestion
的问题会覆盖第一个函数,而progressBar
函数不会处理
没有显示任何错误,我已经改变了顺序或函数,拥有一个大函数都会得到相同的结果。如果我分别运行这两个函数,这两个函数都能完美工作。它们起源于@onClick上的按钮
我尝试过使用承诺,但效果相同
如何仅在progressBar
完成时强制运行nextQuestion
功能
进度条
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){
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);
},
以下内容是否符合您的要求
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);
};
};
},
progressBar:函数(项){
this.percentQuestion=100/this.numberQuestions;
变量变量={
元素:document.getElementById(“myBar”),
numericWidth:elem.style.width.replace(/\D/g'),
currentWidth:+numericWidth,
newWidth:+numericWidth++this.percent问题,
};
//这不是最好的方法,但应该有效
var=这个;
var id=setInterval(函数(){
框架(项目、变量);
}, 10);
功能框架(项目、变量){
if(variables.currentWidth
以下操作是否符合您的要求
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);
};
};
},
progressBar:函数(项){
this.percentQuestion=100/this.numberQuestions;
变量变量={
元素:document.getElementById(“myBar”),
numericWidth:elem.style.width.replace(/\D/g'),
currentWidth:+numericWidth,
newWidth:+numericWidth++this.percent问题,
};
//这不是最好的方法,但应该有效
var=这个;
var id=setInterval(函数(){
框架(项目、变量);
}, 10);
功能框架(项目、变量){
if(variables.currentWidth
尝试添加此.nextQuestion(项目)代码>函数内部frame()
var self=this;
函数框架(){
如果(当前宽度<新宽度){
currentWidth++;
elem.style.width=currentWidth+'%';
}否则{
清除间隔(id);
}
下一个问题(项目);
};
由于frame()
是在setInterval()中执行的this.nextQuestion(项目)
将在每个间隔结束时都有this.nextQuestion(项目)
。尝试添加this.nextQuestion(项目)代码>函数内部frame()
var self=this;
函数框架(){
如果(当前宽度<新宽度){
currentWidth++;
elem.style.width=currentWidth+'%';
}否则{
清除间隔(id);
}
下一个问题(项目);
};
由于在setInterval()
中执行frame()
,您将在每个间隔结束时有此.nextQuestion(项)
。您可以使用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);
您可以使用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);
先将其存储在变量中,然后再移动
this.nextQuestion(项目)代码>到
if(currentWidth };代码>先将其存储在变量中,然后再移动
this.nextQuestion(项目)代码>到
if(currentWidth
};
运行JSFIDLE创建您的场景会很有帮助。我不完全确定您的意思,但看起来您应该将这个.nextQuestion(项)
放在其他
中的clearInterval(id)之后
。运行JSFIDLE创建您的场景会很有帮助。我不完全确定您的意思,但看起来您应该将这个.nextQuestion(项)
放在其他
中,在clearInterval(id)
之后。感谢您的帮助。。。我现在得到一个控制台错误this.nextQuestion不是一个函数
我已经将this
移动到函数外部的一个变量,并将项传递到frame()
中,但是现在得到这个错误无法读取null的属性“style”
。在nextQuestion
中没有到达Like项,只是编辑了答案,将var=this
添加到frame()函数之外。这是一个有点黑客,但它将有望做你想要的。对不起,只是阅读你的评论正确。尝试第二次编辑。我还没有检查它,但我相信它应该可以让您访问项目
。谢谢,现在它会移动到nextQuestion
,但不会处理progressBar
,因此会移动进度条。你知道为什么会这样吗?谢谢你。。。我现在得到一个控制台错误this.nextQuestion不是一个函数
我已经将this
移动到函数外部的一个变量,并将项传递到frame()
中,但是现在得到这个错误无法读取null的属性“style”
。在nextQuestion
中没有到达Like项,只是编辑了答案,将var=this
添加到fr之外