www.webdeveloper.com
Results 1 to 13 of 13

Thread: I need a counter script....

  1. #1
    Join Date
    Jan 2011
    Posts
    6

    I need a counter script....

    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??

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    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.

    ^_^

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    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

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    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.

  5. #5
    Join Date
    Jan 2011
    Posts
    6
    To all users, I do have access to MySQL db if that would do it. I just have no idea how to do this.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    The larger question is, do you want all users to have the same counts or each user to have their own specific one?

  7. #7
    Join Date
    Jan 2011
    Posts
    6
    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.

  8. #8
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    What server-side dynamic language are you using?

    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.

    ^_^

  9. #9
    Join Date
    Jan 2011
    Posts
    6
    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.

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    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).

    The following would help for a flat file version:
    fopen
    flock
    fgets
    fputs
    fclose

  11. #11
    Join Date
    Jan 2011
    Posts
    6
    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.

  12. #12
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    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

    ^_^

  13. #13
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by WolfShade View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles