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

…衆ロ難τιáo~ 提交于 2019-12-17 05:15:18

问题


I have a recursive SetTimeout function that clicks a filter on my page after the filters have loaded (they're loaded through Ajax, so not available immediately on page load).

$scope.clickFilter = function () {
    var filter = $('.filter-item')
        .find('input[value="' + $scope.activeFilter + '"]');

    if (filter.length < 1) {
        setTimeout($scope.clickFilter(), 1000);
    } else {
        $(filter).trigger("click");
    }
}

However, when the filters take a long time to load, I get "Uncaught RangeError: Maximum call stack size exceeded(…)"

How do I prevent this and make sure it runs until completion?


回答1:


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.



来源:https://stackoverflow.com/questions/40913343/maximum-call-stack-size-exceeded-on-settimeout-recursive-function-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!