-2

I am encountering a strange bug when I'm attempting to recursively call a method.

Its as if the compilers skips the setTimeout function.

I am calling the function from another function to start the recursion but the setTimeout doesn't call startFaceTracking() again.

I'm probably being stupid but can someone point it out the issue?

I have tried this,

function startFaceTracking() {

    var eventHit = false;


    if(!eventHit){
        setTimeout(startFaceTracking,500);

    } 
}

and this

function startFaceTracking() {

    var eventHit = false;


    if(!eventHit){
        setTimeout(function(){startFaceTracking();},500);

    } 
}

Thanks in advance,

howells699
  • 148
  • 14
  • 3
    The setTimeout is probably running. Your function doesn't do anything else though, so you wouldn't even know if it's running. What are you expecting to see? – Carcigenicate Dec 18 '17 at 13:02
  • `var eventHit = false;` so your code will call same function repeatedly – Kalaiselvan Dec 18 '17 at 13:03
  • The function definitely isn't running. Otherwise i'd be in an infinite loop. – howells699 Dec 18 '17 at 13:04
  • write a console.log("SOMETHING"); into your timeOut function and check if print it. Probably is like say @Carcigenicate and the setTimeOut functions is working but 500 milseconds is pretty short time to appreciate it. – Mario Junior Torres Perez Dec 18 '17 at 13:05
  • @howells699 How would you even know if there was an infinite loop? The code you posted has no output. And are you ever calling the function in the first place? See the answer below if you aren't. – Carcigenicate Dec 18 '17 at 13:06
  • @MarioJuniorTorresPerez I know what you mean but I do not have the console outputs. Thanks – howells699 Dec 18 '17 at 13:09
  • @Carcigenicate Yes, I am calling the method in the firstplace. The problem is when I call it from within itself using setTimeout() . Thanks – howells699 Dec 18 '17 at 13:10
  • 1
    @howells699 you could try this... write eventHit outside the startFaceTracking() function like a global variable with false value and copy the if statement into startFaceTracking() function and paste below the function. Because I assumed that you're invoked the function somewhere, if not, is probably is not working for this reason... because you dont invoke the startFaceTracking() function in nonewhere – Mario Junior Torres Perez Dec 18 '17 at 13:18

3 Answers3

3

You can use an immediately invoked function expression (also known as IIFE) like this:

(function startFaceTracking() {

  var eventHit = false;


  if(!eventHit){
    setTimeout(startFaceTracking,500);

  } 
})();

You can read more here: Ben Alman about Immediately-Invoked Function Expression (IIFE)

Dipiks
  • 3,818
  • 2
  • 23
  • 39
2

Your timeout is inside the function, and you're not calling that function, so the timeout is never initiated in the first place. Call it once, then it will recurse.

function startFaceTracking() {
  console.log("Starting face tracking!")

  var eventHit = false;

  if (!eventHit) {
    console.log("Starting timeout...");
    setTimeout(startFaceTracking, 500);
  }
}

startFaceTracking()
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Is it possible to make the method recursive and use the setTimeout function? Thanks – howells699 Dec 18 '17 at 13:07
  • @howells699 then we have a duplicate of https://stackoverflow.com/questions/6685396/execute-the-setinterval-function-without-delay-the-first-time – Salman A Dec 18 '17 at 13:08
0

The issue was I was using I was using a setTimeout inside of a setTimeout.

I had badly formatted my java script and tested functions also.

Here is the link to the answer.

Sorry to mess everyone around. I could have typed up a better description of my issue.

Thanks again,

howells699
  • 148
  • 14