Maximum call stack size exceeded on SetTimeout recursive function (Javascript) [duplicate]

The problem is here:

  setTimeout($scope.clickFilter(), 1000); 

Putting () after the function reference means that you want the function to be called, immediately, at that point in the code. What you probably want instead is something like:

  setTimeout($scope.clickFilter.bind($scope), 1000);

which will

  • pass a function reference to setTimeout(), as is required, and
  • ensure that the function will be invoked with the proper this value (what the .bind() part does)

Once you get it working, the term “recursive” isn’t really appropriate. Yes, the function is referencing itself when it arranges for the call after the timer expires, but it’s not directly calling itself; it’s asking something else (the timer mechanism) to call it later.

Leave a Comment