How do I clear this setInterval inside a function?

The setInterval method returns a handle that you can use to clear the interval. If you want the function to return it, you just return the result of the method call: function intervalTrigger() { return window.setInterval( function() { if (timedCount >= markers.length) { timedCount = 0; } google.maps.event.trigger(markers[timedCount], “click”); timedCount++; }, 5000 ); }; var … Read more

find the time left in a setTimeout()?

Just for the record, there is a way to get the time left in node.js: var timeout = setTimeout(function() {}, 3600 * 1000); setInterval(function() { console.log(‘Time left: ‘+getTimeLeft(timeout)+’s’); }, 2000); function getTimeLeft(timeout) { return Math.ceil((timeout._idleStart + timeout._idleTimeout – Date.now()) / 1000); } Prints: $ node test.js Time left: 3599s Time left: 3597s Time left: 3595s … Read more

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 … Read more

Is setTimeout with no delay the same as executing the function instantly?

It won’t necessarily run right away, neither will explicitly setting the delay to 0. The reason is that setTimeout removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue. console.log(1); setTimeout(function() {console.log(2)}); console.log(3); console.log(4); console.log(5); //console logs 1,3,4,5,2 for more details see http://javascriptweblog.wordpress.com/2010/06/28/understanding-javascript-timers/

Get return value from setTimeout [duplicate]

You need to use Promises for this. They are available in ES6 but can be polyfilled quite easily: function x() { return new Promise((resolve, reject) => { setTimeout(() => { resolve(‘done!’); }); }); } x().then((done) => { console.log(done); // –> ‘done!’ }); With async/await in ES2017 it becomes nicer if inside an async function: async … Read more

What advantage is there in using the $timeout in AngularJS instead of window.setTimeout?

In basic words $timeout refers to angularjs when setTimeout – to JavaScript. If you still think to use setTimeout therefore you need invoke $scope.$apply() after As a side note I suggest you to read How do I “think in AngularJS” if I have a jQuery background? post and AngularJS: use $timeout, not setTimeout Example 1: … Read more