0

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;
    }
Curious
  • 889
  • 1
  • 9
  • 21

4 Answers4

2

You need to put the code in the set time out for it to wait but its a bit tricky because your return will not work

you can use promise to a chive that. i haven't tested this code so it will probably wont work but this is the direction you need to take

function matrixSearch(array,row1,row2,col1,col2,value)
    {
    return  new Promise(reslove,reject){
      setTimeout(function() {
        console.log(array[row1][col1]);
        console.log(array[row2][col2]);
        if(value<array[row1][col1] || value > array[row2][col2])
            return resolve(false);
        if(value==array[row1][col1] || value == array[row2][col2])
            return resolve(true);
         var cRow1=row1,cCol1=col1,cRow2=row2,cCol2=col2;
         var midRow = Math.floor((row1+row2)/2);
         var midCol = Math.floor((col1+col2)/2);
        
           while((row1!=midRow || col1!=midCol) && (row2!=midRow || col2!=midCol))
              {
                if(array[midRow][midCol]==value)
                {
                      return resolve(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);
              }
              matrixSearch(array,midRow+1,cCol1,cRow2,midCol,value)
              .then(function(found) {
                if(found === false){
                   found = matrixSearch(array,cRow1,midCol+1,midRow,cCol2,value);
                  }
              })
             
         },100);
      
    }
Amit Wagner
  • 2,370
  • 3
  • 9
  • 28
1

setTimeout is passed a callback function which executes after the given time. The code that follows will execute immediately following the setTimeout line, not after the specified timeout.

If you want the code block to execute after the time period, you need to put it in the callback function.

Andrew H
  • 383
  • 2
  • 8
  • But how shall i achieve something like sleep. – Curious Sep 29 '17 at 16:08
  • Javascript behaves slightly differently then Java/C. Your timeout basically puts that passed function into an event loop which is passed over and over until the stated time has passed. There is no equivalent to sleep. – Andrew H Sep 29 '17 at 16:10
1

setTimeout is not for blocking function execution, it's for delaying execution of callback function passed to setTimeout function, for delaying rest of the function you must put it where console.log is placed right now

erhardos
  • 148
  • 1
  • 8
  • In such case how shall i block execution of whole recursive function execution. – Curious Sep 29 '17 at 16:10
  • 1
    i dont recall any mothod to do this in es5, if you can use newer varsion of js u could use https://stackoverflow.com/a/39914235/6671416 – erhardos Sep 29 '17 at 16:18
1

First, setTimeout uses milliseconds, so for a 100 second sleep, you need to use 100000 instead of 100.

Secondly, setTimeout is asynchronous, which means the the callback function is scheduled to execute after the time set has elapsed, but other code is still executed in the meantime.

To do what you want, you need a dummy variable, and a while-loop condition that tests for the state of this variable:

proceed=false;
setTimeout('proceed=true;',100000);
while (!proceed) {}
// rest of code

The while loop will hold code up until the setTimeout proceed=true; code is executed.

JMP
  • 2,299
  • 17
  • 26
  • 34