【发布时间】:2018-03-11 14:13:36
【问题描述】:
我有一个如下的 javascript 函数。我想在 setTimeout 方法处暂停执行。据我所知,setTimeout 方法没有按预期工作,执行过程超出了 setTimeout 方法,并且在 100 秒后调用了 console.log("wait")。但这不是我所期望的,我想睡 100 秒。我将如何实现这一点,也可能是因为递归调用 console.log("wait") 只被调用一次,而不是所有递归调用。为什么?
function matrixSearch(array,row1,row2,col1,col2,value)
{
console.log(array[row1][col1]);
console.log(array[row2][col2]);
if(value<array[row1][col1] || value > array[row2][col2])
return false;
if(value==array[row1][col1] || value == array[row2][col2])
return true;
var cRow1=row1,cCol1=col1,cRow2=row2,cCol2=col2;
var midRow = Math.floor((row1+row2)/2);
var midCol = Math.floor((col1+col2)/2);
setTimeout(function() {
console.log("wait");
},100);
while((row1!=midRow || col1!=midCol) && (row2!=midRow || col2!=midCol))
{
if(array[midRow][midCol]==value)
{
return true;
}
if (array[midRow][midCol]<value)
{
row2=midRow;
col2=midCol;
}
else
{
row1=midRow;
col1=midCol;
}
midRow = Math.floor((row1+row2)/2);
midCol = Math.floor((col1+col2)/2);
}
var found = matrixSearch(array,midRow+1,cCol1,cRow2,midCol,value);
if(!found)
found=matrixSearch(array,cRow1,midCol+1,midRow,cCol2,value);
return found;
}
【问题讨论】:
标签: javascript settimeout