I'll have more than one of these small boxes on my site, and each will start counting down at different times.
How can I decrease the numerical value of the timer per second, giving the simulation of a countdown timer?
<p class="countdown">15</p>
Using this javascript it correctly countsdown, but every single auctionbox is affected. How would you suggest I isolate the timer to act on only one item?
<script>
var sec = 15
var timer = setInterval(function() {
$('.auctiondiv .countdown').text(sec--);
if (sec == -1) {
$('.auctiondiv .countdown').fadeOut('slow');
clearInterval(timer);
}
}, 1000);
</script>
Try this in your inspector to get the idea how a countdown timer should work:
And here is full code for your case(no jquery)
You don't need jQuery for this (though it will help in setting the text).
setInterval
is what you want.CountdownTimer is a reverse count down jQuery plugin for displaying countdown as per need with its different configuration options.
A minimal jQuery countdown clock plugin which allows you to count down to a target date time with the support of custom UTC Timezone offset.
How to use it:
jQuery library and the jQuery countdown clock plugin in your web page.
Create the Html for a countdown clock using unordered list.
Add the following CSS snippets to style the countdown clock.
Try the following which will properly issue the count down for the selected values.
JSFiddle Example
Note: This will run the count down sequence on every element which has the
countdown
class. If you'd like to make it more restrictive to a single element you'll need to alter the selector from.countdown
to something more restrictive. The easiest way is to add an id and reference the item directly.The JavaScript is a little more complex here because you'll want the timer to eventually shut off since there's not much chance, or use, of element with a duplicate id being added
JSFiddle Example
HTML:
JS:
Fiddle: http://jsfiddle.net/wwvEn/