I have a counter snippet and right now it’s set so it counts down from 7 every 1000 ms’s but what i want it to do is
- not animate
- when the user clicks refresh it goes down by one, so it’s static.
here is part of the code that i have at the moment and the rest is in it’s core javascript file which it gets some of it’s syntax from.
<script type="text/javascript">
var myCounter = null
var timerId = null
function loadCounter() {
myCounter = new Counter("my_counter",{
digitsNumber: 2,
direction: Counter.ScrollDirection.Upwards,
characterSet: Counter.DefaultCharacterSets.numericUp,
charsImageUrl: "_images/numeric_up_blackbg5.png",
markerImageUrl: "_images/marker.png"
});
myCounter.value = 7;
timerId = window.setInterval("myCounter.setValue(parseInt(myCounter.value) - 1, 450);", 1000);
}
loadCounter();
</script>
I’m pretty inexperienced at javascript so if you could be thorough that would be fantastic. Thank you very much 🙂
As the comments suggested your javascript variables will be cleared each time the page is refreshed. So you cannot store the counter in a JS variable.
You have two options
1) if you using a severside technology such as PHP, ASP etc you could store it in a text file.
2) use a cookie. This is probably the easiest approach.
So the first time the user visist the page check if a cookie exist. If it does then you can incremember the counter in it and check if it equals 7.
If it does not exist create one you probably want to use a Session Cookie so that it expires once t he user closes their browser assuming you do not want to keep a counter if a user visited the page one week ago and comes back again today. See here for some information about how to read and write session cookies http://www.javascriptkit.com/javatutors/cookie.shtml