How to limit the number of iterations done by setInterval

I show video ads to my users. I do not place these ads, by the way; I get them from another company.

When a click on an ad, it leaves a cookie in the user’s browser. I created a function that checks for the existence of a cookie every 10 seconds.

What I would like to do is to limit the number of starts of this function or the number of seconds in which it can work.

Below is the function:

function checkCookie() { var cookie=getCookie("PBCBD2A0PBP3D31B"); if (cookie!=null && cookie!="") { alert("You clicked on an ad" ); } setInterval("checkCookie()", 10000); 

So, to repeat. I want to limit the number of iterations that setInterval("checkCookie()", 10000); can do

+7
source share
5 answers

When you call setInterval , it returns the interval identifier, which you can then use to stop it by calling clearInterval . So you want to count the iterations in the variable, and once they reach a certain amount, use clearInterval with the identifier provided by setInterval .

 var iterations = 0; var interval = setInterval(foo, 10000); function foo() { iterations++; if (iterations >= 5) clearInterval(interval); } 

Living example

+11
source

This should do it:

 function checkCookie() { var cookie = getCookie("PBCBD2A0PBP3D31B"); if (cookie != null && cookie != "") { alert("You clicked on an ad"); } if (counter > 10) clearInterval(clr); counter++; clr = setInterval(function(){checkCookie()}, 10000);​ } var counter = 0; checkCookie(); 
+2
source

WindowTimers.setInterval(func, delay[, param1, param2, ...])

The parameter 3 rd and further in setInterval are optional parameters for transition to the interval function. Please note: these optional arguments are not supported in IE9 and earlier.

We can use this to our advantage, avoiding the use of a global or external sphere. as shown below. The interval function monitors the limit and current increment of the counter via the opts parameter.

The runTask function takes a mandatory argument fn , which returns a boolean value to determine if a timer has been completed. In the example below, there are two tixes, each of which depends on the speed with which they are running and the state that should be running.

The first two tasks complete, but the last ends with attempts before the condition is met.

 function writeLine(el, text) { el.innerHTML += [].slice.call(arguments, 1).join(' ') + '\n'; } function runTask(options, interval, limit) { var interval = setInterval(function(opts) { opts.incr = (opts.incr || 0) + 1; if (opts.fn(opts)) { clearInterval(interval); writeLine(opts.el, '>> Task finished...'); } else if (opts.incr > limit) { clearInterval(interval); writeLine(opts.el, '>> Exceeded limit of ' + limit); } else { writeLine(opts.el, '>> Attempt: ' + opts.incr + '/' + limit); } }, interval, options); } // 5 atttempts to reach 4 in 250ms. runTask({ fn : function(opts) { return opts.incr === 4; }, el : document.querySelectorAll('div.col')[0] }, 250, 5); // 10 atttempts to reach 7 in 100ms. runTask({ fn : function(opts) { return opts.incr === 7; }, el : document.querySelectorAll('div.col')[1] }, 100, 10); // 10 atttempts to reach 15 in 50ms. runTask({ fn : function(opts) { return opts.incr === 15; }, el : document.querySelectorAll('div.col')[2] }, 50, 10); 
 .col { display: inline-block; width: 175px; font-family: monospace; white-space: pre; border: thin solid black; vertical-align: top; padding: 4px; } 
 <div class="col"></div> <div class="col"></div> <div class="col"></div> 
+1
source

You just need some kind of global counter variable to track. For example, the following code will check cookies no more than 20 times per page load.

 var numChecks = 0; function checkCookie() { ... numChecks++; if (numChecks < 20) setTimeout("checkCookie()", 10000); } setTimeout("checkCookie()", 10000); 
0
source

Passing a callback to an interval function, which in turn updates the counter in the global area:

 var countIntervals = 0, intervalFunc = function(_callback){ console.log(countIntervals); if(countIntervals > 5) { clearInterval(setIntervalVar); } else { // do stuff _callback(); } }; setIntervalVar = setInterval(intervalFunc.bind(null, function(){ countIntervals++; }), 500); 
0
source

All Articles