I display video ads to my users. I don’t host these ads by the way; I get them from another company.
When ever an ad is clicked it leaves a cookie in the user’s browser.
I’ve created a function that checks the existence of a cookie every 10 seconds.
What I would like to do is to limit the number of times this function can run or the number of seconds it can run for.
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 recap. I want to limit the number of iterations that setInterval("checkCookie()", 10000); can make
When you call
setInterval, it returns you an interval ID that you can then use to stop it by callingclearInterval. As such, you’ll want to count the iterations in a variable, and once they’ve reached a certain count, useclearIntervalwith the ID provided bysetInterval.Live example