There are literally thousands of sites out there that have free code for all kinds of counters. I'm almost positive you could even do a search on this site and get many examples.
If you want to write your own custom code, it may be too much of an undertaking for an inexperienced coder at this point. But when there are many free sources, no need to do that.
If this actually concers an actual application and not just some toy example to learn, do not rely on JavaScript only. Anyone can manipulate it and put in any price they want.
This is not suitable for Javascript, as in any case the time is the time as per the user's browser.
You will need to use server-side code for this so that all users see the same price. Otherwise if I advance my computer clock by one hour I get the reduced price which will become applicable an hour in the future!
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
This is not suitable for Javascript, as in any case the time is the time as per the user's browser.
You will need to use server-side code for this so that all users see the same price. Otherwise if I advance my computer clock by one hour I get the reduced price which will become applicable an hour in the future!
Or if your website is in the USA, Philip doesn't have to do anything...as he is in the UK and several hours ahead. Or if your website is in the UK, then the same applies to, say, people in India.
etc.
It's nutso to depend on JavaScript for something like this!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
This is for a car dealer website. We have a Dutch auction running and u want to display this online with the price decreasing $3 per hour until it sells. I was going to put a disclaimer that said the price shown on the computer when they visit the showroom is the final price they could purchase the car for so the price would be verified in person before purchase. I still don't see an example on the above searches for a price countdown.
<html>
<head>
<style type = "text/css">
.showred {color:red; font-size:200%; font-weight:bold;}
</style>
</head>
<body>
<span id = "theprice" class = "showred" ></span>
<script type = "text/javascript">
function reducePrice() {
var sd = new Date ("February 18th 2013 9:00"); // the starting date/time
var sdt = sd.getTime();
var p = 5000; // the starting price;
var now = new Date().getTime();
var diff = now - sdt;
var diffhrs = Math.floor(diff/1000/60/60); // complete hours after start date/time
var newprice = p - (diffhrs * 3);
if (p < 0) {p=0} // not to be negative! You can set a minimum below which the price cannot fall.
document.getElementById("theprice").innerHTML = "This wonderful car is now on sale at $" + newprice;
}
reducePrice();
setInterval(reducePrice, 60000); // update every minute
</script>
</body>
</html>
If you want the price to go down at the rate of $0.05 per minute rather than $3.00 per hour:-
Code:
<html>
<head>
<style type = "text/css">
.showred {color:red; font-size:200%; font-weight:bold;}
</style>
</head>
<body>
<span id = "theprice" class = "showred" ></span>
<script type = "text/javascript">
function reducePrice() {
var sd = new Date ("February 18th 2013 9:00"); // the starting date/time
var sdt = sd.getTime();
var p = 5000; // the starting price;
var now = new Date().getTime();
var diff = now - sdt;
var diffmins = Math.floor(diff/1000/60); // minutes after start date/time
var newprice = (p - (diffmins * .05)).toFixed(2);
if (p <0 ) {p=0} // not to be negative!
document.getElementById("theprice").innerHTML = "This wonderful car is now on sale at $" + newprice;
}
reducePrice();
setInterval(reducePrice, 30000); // update every 30 seconds
</script>
But I would look at your legal position. You must make it clear that price shown on the site is only an invitation to treat and the prospective purchaser must attend the showroom.
We are sorry that as a result of a typographical error we referred to General X as "a bottle-scarred old veteran". This should of course have read "a battle-scared old veteran".
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.