JavaScript的使用功能,而循环条件

全球

定义的变量:JavaScript的使用功能,而循环条件

var domArray = [] //will have multiple string 

var onQueueDom = [] //will have 1 string only

var onQueueDomStatus = ["N"] //will one of the status: "N", "P","D"

var processedNum = 0

我创建这将返回真或假,它会等待3秒只运行,如果其他功能:

function checkIfPending(){ 

console.log('checkIfPending being executed ')

setTimeout(function(){

if(onQueueDomStatus[0] == "D"){

console.log("Process Done, returning True")

return true

console.log("True has been returned, you shouldn't seeing this")

}

else if(onQueueDomStatus[0] !== "D" || onQueueDomStatus[0] !== "N" && onQueueDom !== ""){

console.log("Still Processing, will re-check in 3 second")

return false

}

else {

console.log("No domain on Queue but status not clear")

console.log("Clearing status...")

onQueueDomStatus[0] = "D"

console.log('setting onQueueDomStatus to ' + onQueueDomStatus[0])

console.log("Status changed to D & returning True")

return true

}

}, 3000);

}

我想使用上述函数作为while循环的条件,但它不会处理While循环中的代码,即使onQueueDomStatus [0] ==“D”:

while(checkIfPending() == true){ 

console.log('while loop is running')

onQueueDomStatus[0] = "N"

console.log('setting onQueueDomStatus to ' + onQueueDomStatus[0])

movetoQueue()

console.log('Executing movetoQueue')

}

假设onQueueDomStatus [0]始终是“D”,但它仍然不起作用。

问题:while循环会每次等待3秒钟执行吗?

回答:

checkIfPending没有返回值,所以调用它会导致值undefined。你传递的回调setTimeout有一个返回值(虽然setTimeout忽略它),但checkIfPending没有。 checkIfPending不能返回它来自异步操作(more here)的任何值。

while是同步控制流程结构*不能将异步函数的结果用作while中的条件。

编辑:gurvinder372有done a good job向您展示如何重组事物来处理异步性。


*的while的语义可以异步做出了ES2017 + async功能里面如果您使用await,但在幕后,真正的情况是,该功能被重写不使用while

回答:

您需要调用,而不是返回truefalse

function checkIfPending(exitCallback, trueCallback){ 

console.log('checkIfPending being executed ')

setTimeout(function(){

if(onQueueDomStatus[0] == "D"){

trueCallback(); //invoke callback which signals that checkPending should continue

checkIfPending(exitCallback, trueCallback);

}

else if(onQueueDomStatus[0] !== "D" || onQueueDomStatus[0] !== "N" && onQueueDom !== ""){

exitCallback();

}

else

{

onQueueDomStatus[0] = "D";

trueCallback(); //invoke callback which signals that checkPending should continue

checkIfPending(exitCallback, trueCallback);

}

}, 3000);

}

回调,并以此作为

checkIfPending(function(){ 

//console.log("while loop ended");

}, function(){

onQueueDomStatus[0] = "N";

movetoQueue();

})

回答:

此代码将帮助您

function getPromise(){  

\t return new Promise((resolve,reject)=>{

\t setTimeout(function(){

if(onQueueDomStatus[0] == "D"){

console.log("Process Done, returning True")

resolve(true)

}

else if(onQueueDomStatus[0] !== "D" || onQueueDomStatus[0] !== "N" && onQueueDom !== ""){

console.log("Still Processing, will re-check in 3 second")

resolve(false)

}

else {

console.log("No domain on Queue but status not clear")

console.log("Clearing status...")

onQueueDomStatus[0] = "D"

console.log('setting onQueueDomStatus to ' + onQueueDomStatus[0])

console.log("Status changed to D & returning True")

resolve(true)

}

}, 3000);

})

}

async function final(){

\t var d = await getPromise(); // d will contain boolean after specified delay

\t while(d == true){

\t \t onQueueDomStatus[0] = "N"

\t \t movetoQueue();

\t \t d = await getPromise(); \t

\t }

}

以上是 JavaScript的使用功能,而循环条件 的全部内容, 来源链接: utcz.com/qa/261302.html

回到顶部