java script execute code on every 10th vistor

espressox

Registered Member
Joined
Apr 24, 2011
Messages
75
Reaction score
7
is there anyway to code a piece of JS (very basic) to only execute/work on every 10th or 100th or what ever visitor/hit to to your webpage
I would want to put it in an if else statement but cant find away of setting this limitation
 
You can declare a variable that will be incremented every time a unique visitor (determined by his IP and UserAgent for example) hits your front page or whatever. Then you can check this visit's number and trigger the wanted bihavior for x visit.

the ideal approach will be using redis for storing your count.
 
This has to be done at server level. Your server needs to track the visitors to determine if your JS code executes or not.
 
Have a callback on your page that makes an ajax call. Count +1 on that ajax call. Then when it reaches the count, reset and do what you need. Although it won't be that good when you have parallel visitors. Still... You said you need JS, I gave you a solution for JS. :p

IMHO, it is best processed in the server side (do the exact same, but from server side).

Edit:
This has to be done at server level.
This guy has faster fingers than me. :D

You can declare a variable that will be incremented every time a unique visitor (determined by his IP and UserAgent for example) hits your front page or whatever. Then you can check this visit's number and trigger the wanted bihavior for x visit.

the ideal approach will be using redis for storing your count.
Huh, variable as in? JS variable? It will not remember anything after one page view. :p
 
if (Math.floor(Math.random() * 10) + 1 == 1) {
your code here
}

No serverside stuff needed ;) Over time it will average out to execute on every 10th user.
 
If you are actually stuck with no server-side access there are some free cache providers like Memcachier, Firebase, etc that you can post your increment to and receive the current count.
 
if (Math.floor(Math.random() * 10) + 1 == 1) {
your code here
}

No serverside stuff needed ;) Over time it will average out to execute on every 10th user.

Ha, this is actually pretty good provided you don't need to be super accurate! Way less work than interacting with a cache.
 
Ha, this is actually pretty good provided you don't need to be super accurate! Way less work than interacting with a cache.
The way I think it can be done easily is by using the database. Or some file. His solution won't be accurate. :)
For accuracy, definitely store the counter in db table or a file (file has more problems than db).
 
Server side ofc... ( he was talking JS, i assumed it was Node.js )
Ahh ok I give u the benefit of doubt (weird how the client side language got into server side but... let's not debate that). :p

You would still need a global storage like the db, or a file though.
 
The way I think it can be done easily is by using the database. Or some file. His solution won't be accurate. :)
For accuracy, definitely store the counter in db table or a file (file has more problems than db).
Eh it depends. If the site gets a lot of traffic (e.g. 10 per second) this would easily be accurate enough. Also if he just wanted the code to execute for every 10% of users, it will do exactly that. It wasn't really clear from his question if that's enough for him.
 
Do you need this to redirect traffic for those who rip your lander?
 
Eh it depends. If the site gets a lot of traffic (e.g. 10 per second) this would easily be accurate enough. Also if he just wanted the code to execute for every 10% of users, it will do exactly that. It wasn't really clear from his question if that's enough for him.
Still, it's a random number. He is not executing it every 10th request. It's not the same, ultimately.

Statistically though, it might actually be close to the same depending on the number of execution and accuracy.
 
Still, it's a random number. He is not executing it every 10th request. It's not the same, ultimately.

Statistically though, it might actually be close to the same depending on the number of execution and accuracy.
Yes, like I said, it depends on what he uses it for and how often it gets executed.
 
OK damn thats a lot of posts what I am trying to do is redirect a page after every 100th visitor
I thought about doing the random number thing but was hoping for something better and yes Unique IP is what I am thinking of
The aim is to limit the traffic to a certain offer page
would a counter work ie count 1-100 and execute code at 100 and reset to 1 again
 
Back
Top