I am looking for a count up script that increments by one every 20 mins, and I also need it to cache the amount. I have found several scripts that will increment by one in a certain time fram but none of them keep the count going, they all set back to 0 when the page is refreshed. Can anyone help??
Cookies might be the only thing I can think of, off the top of my head, that will keep the count value, even when the page is refreshed.
Set it so that it checks for the cookie to exist, first; if it does, go to next step - if it doesn't, create it then go to next step (make it contain a timestamp plus an integer.) Have the function check the value of the count every minute (60,000 milliseconds) and subtract the stated timestamp from the current time - if it's more than 20 minutes, increase the count by one and set the value of the cookie, again, with the new count value and the current timestamp.
This is what I have no...and I have no idea what to use to get it to remember the last count...can you please help????
Code:
Every 20 mins this number increases by one<span id="number">10</span> ...see !!!</span>
<script type="text/javascript">
var i = 10;
function increment() {
i++;
document.getElementById('number').innerHTML = i;
}
setInterval('increment()', 1200000);
</script>
1200000 being the interval in milliseconds for 20 mins
Are you wanting the count to be specific to each user viewing the page or to all users? If the former you'll need to use cookies or another form of client side storage. If the latter you'll need to employ a server-side language to store the value and feed the starting value to the javascript when it is loaded.
Need all users to see the same count. And if a new user comes to the site they get the current count as well. Basically I just need it to start at 10 and continue going up once I have it on the site for ever. Just do not need it resetting on a page refresh. I guess the best way to put this is a static counter that always goes up and never resets. Sorta like a page hit counter, just not using page hits, just increments by one every 20 mins.
ColdFusion has the ability to schedule .cfm documents to run at any specified interval. If you are using ColdFusion, you can create a page that updates a single column in the database by one, then schedule it to run once every twenty minutes.
If you're using another dynamic language, you'll have to look up scheduling a document to run.
everything on the site is PHP, it is actually in a Joomla site. If you could point me in the direction of trying to figure out how to get this done thru the DB I think that would be the best way to go. Because cookies are not going to really help at all I think.
Might be easiest to use a simple flat file counter (or just a start time). Then read that using PHP to calculate/echo the information to the javascript. Alternatively you could use a database (though that seems like overkill for a simple counter).
I do not understand what all that is..., it would seem to me to have this keeping the count it would need to be reading from somewhere. So my assumption to go with the DB would be best, I have even looked at text based but cannot get anywhere.
You might do it with a flat file on the server, but you'd have to throw in conditionals to make sure it wasn't updated more than once every twenty minutes - if you had 100 users all accessing it at approximately the same time, they aren't all going to hit the twenty minute mark at the same time.
Database is the better option. Create a table, call it "JS_COUNTUP", give it a single column (or more, if you want) named "thisCount" that is an integer datatype, and set it to 0. Create a document that can be scheduled to run once every 20 minutes and have it query the database to up the count.
You might do it with a flat file on the server, but you'd have to throw in conditionals to make sure it wasn't updated more than once every twenty minutes - if you had 100 users all accessing it at approximately the same time, they aren't all going to hit the twenty minute mark at the same time.
Database is the better option. Create a table, call it "JS_COUNTUP", give it a single column (or more, if you want) named "thisCount" that is an integer datatype, and set it to 0. Create a document that can be scheduled to run once every 20 minutes and have it query the database to up the count.
UPDATE JS_COUNTUP
SET thisCount = thisCount + 1
^_^
You could also call an increment script using cron once every 20 minutes to increment the value in the flat file.
Another thought is to just use a start time and add 1 for every 20 minutes that has passed when the page is called. Then you'd have the start count for the javascript without the need to update anything in a database or flat file.
Date should help you with that if that is how you wanted to do it.
Bookmarks