How to implement the code after function? It should invoke function only after the third time
function after(count, funct) {
// code...
}
var called = function () {
console.log("hello");
};
var afterCalled = after(3, called);
afterCalled(); // --> nothing is printed
afterCalled(); // --> nothing is printed
afterCalled(); // --> "hello" is printed
You could return a function which decrements the counter and if zero it calls the handed over function.
function after(count, funct) {
return function () {
if (!--count) funct();
};
}
var called = function () {
console.log("hello");
};
var afterCalled = after(3, called);
afterCalled(); // --> nothing is printed
console.log('.')
afterCalled(); // --> nothing is printed
console.log('.')
afterCalled(); // --> "hello" is printed
console.log('.')
afterCalled(); // --> nothing is printed