I had a javascript function as below. I want to pause the execution at setTimeout method. The setTimeout method is not working as expected as far i understand the execution pass beyond the setTimeout method and console.log("wait") is called after 100 secs. But that is not what I'm expecting I want to sleep for 100 secs. How shall i achive this and also may be because of recursive call the console.log("wait") is being called only one time, not for all recursive calls. Why ?
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;
}