Forum Moderators: open
What I need is a timer script that:
1. Counts down from what ever time I specify in hh:mm:ss.
2. Stays in sync with the seconds on the server clock.
3. At the click of one button by the user:
a) Advance the timer UP to the next ten-second mark.
b) Add 1 point to a <div> that displays the number of clicks for that button.
c) As that <div> reaches certain various numbers of my choosing, change the contents (the prize) displayed in a second <div>.
d) Display (in a third <div>) the username of the user who most recently clicked the button.
4. Replaces the timer with the words "two" "one" when the countdown reaches 2, 1.
5. Checks the server for last-second button clicks when the countdown reaches 0, and:
a) Deactivates the button during this check.
b) Replaces the timer with the word "verifying" during this check.
6. If no last-second clicks are found:
a) Replace the timer with the words "game over".
b) The button remains deactivated.
c) Redirect the winner (the last user to click the button before 0) to another page to claim their prize.
7. Can accomodate as many separate and unique timer/button/<div> setups as I want on a single page.
Whew... that's a lot! This is VERY similar to what is used on several "penny auction" sites. Same idea with some different requirements. So if you want a visual of the idea I'm shooting for, do a google search for "penny auctions" and most of the first few listings should have a similar timer.
I realize a script exactly like this doesn't exist, so if anyone is willing to write it up for me I will think the world of you for taking the time!
Notes within the script would be wonderful, but I'll be happy with what ever I can get. :)
First off, you're going to need to get your server to feed the server time to your page, where JavaScript can grab it and use it. You can't use the current time according to JavaScript, because that's based on whatever the user's computer is set to. Here's one way of inserting the server time into the page, using SSI (server-side includes):
<!--#include virtual="servertime.cgi"-->
The contents of the servertime.cgi file is:
#! /usr/bin/perlprint "Content-type:text/html\n\n";
$servertime = time;
print "<input type=hidden name=servertime value='$time'>";
Now you have a hidden field on the page which JavaScript can access. For example:
<script type=text/javascript>
theServerTime = document.getElementById('servertime').value;
alert(theServerTime);
</script>
There is lots, lots more to your project, but let's make sure you understand everything above before we go further.
Thanks a ton for your reply and the clarification on WW's purpose. Your assumption is totally right that I'd like to learn to do this myself rather than hiring it out, so if you're willing to walk me through it then you rock and I am a sponge.
In that case, though, I'll need to get my site uploaded to a server before I can begin. Which I was going to do anyway since I just finished the design portion. So I'll do that and then we can continue.
I did create a "servertime.cgi" file with the contents you gave and inserted the #include comment into top of the <body> of my source page.
I also made a "servertime.js" file with the script you gave (minus the <script></script>) and pointed to it in the <head>.
However, I get an error with the .js file saying "Object required" on line 1, char 1. Is this just because I'm not on a server yet or did I do something wrong?
You don't actually have to upload your whole site, or even a finished page. You can work with scratch test files.
The first thing is to make sure your SSI (the "include virtual" bit) loads properly. If you've never used SSI's before, you have to add this line to your .htaccess file:
AddHandler server-parsed .htm .html
That tells the server to look for SSI's to load in any file that ends in .htm or .html. This won't work when you view the file locally, since it's your web server that inserts the file. Oh, and your .htaccess file is at the same level as your <index.html> file. If you don't have one, you can create it, it's just a text file. Note that some file transfer programs don't display files that begin with a period, so you might have an .htaccess file but not be able to see it.
Next, you'll need to set the file permissions for your .cgi file to 755 to get it to run. You might be able to do this with your file transfer software, or if you're handy with UNIX, you'd log into your account and type:
chmod 755 servertime.cgi
Moving on, I suggest keeping your JavaScript on the page itself rather than an external file while you're building this project. That way there's only one file to edit. Once you've got everything working the way you want it, then you can stick the JS into a separate file.
Next, yes, you're getting the "Object required" error because you're not running the file on the server. The SSI is going to write the hidden servertime field onto your page, so your JS can access it. But since you didn't run the file from the server, then there is no hidden servertime field, so your JS can't find it, so it's complaining. Best thing is to upload your file to the server and run it there.
Okay, that ought to keep you busy.
<!--#include virtual="includes/servertime.cgi"-->
Webpage error details
Message: Object required
Line: 20
Char: 1
Code: 0
URI: http://example.com/index.php
#! /usr/bin/perl
print "Content-type:text/html\n\n";
$servertime = time;
print "<input type=hidden name=servertime value='$time'>";
#! /usr/bin/perl print "Content-type:text/html\n\n"; $servertime = time; print "";
$time
<input type=hidden name=servertime value=''>
<html>
<!-- #include virtual="includes/servertime.cgi">
<script type=text/javascript>
alert(document.getElementById('servertime').value);
</script>
</html>
print "<input type=hidden name=servertime value='$time' id=servertime>";
<html>
<!--#include virtual="includes/servertime.cgi"-->
<script type=text/javascript language="javascript">
theServerTime = document.getElementById('servertime').value;
alert(theServerTime);
</script>
</html>
#! /usr/bin/perl
print "Content-type:text/html\n\n";
$servertime = time;
print "<input type=hidden name=servertime value='$time' id=servertime>";
<table border=0 cellpadding=5 cellspacing=0><tr>
<td><a href=/><img src=/parts/logo.jpg></a></td>
<td><a href=/>Home</a></td>
<td><a href=/products/>Products</a></td>
<td><a href=/about.html>About Us</a></td>
</tr></table>
<html>
<!--#include virtual="includes/servertime.cgi"-->
<script type=text/javascript language="javascript">
theServerTime = document.getElementById('servertime').value;
alert(theServerTime);
</script>
</html>
<html>
<input type=hidden name=servertime value='1268423838' id=servertime>
<script type=text/javascript language="javascript">
theServerTime = document.getElementById('servertime').value;
alert(theServerTime);
</script>
</html>
<html>
<input type=hidden name=servertime value='' id=servertime>
<script type=text/javascript language="javascript">
theServerTime = document.getElementById('servertime').value;
alert(theServerTime);
</script>
</html>
What I need is a timer script that:
1. Counts down from what ever time I specify in hh:mm:ss.
2. Stays in sync with the seconds on the server clock.
3. At the click of one button by the user:
a) Advance the timer UP to the next ten-second mark.
b) Add 1 point to a <div> that displays the number of clicks for that button.
c) As that <div> reaches certain various numbers of my choosing, change the contents (the prize) displayed in a second <div>.
d) Display (in a third <div> ) the username of the user who most recently clicked the button.
4. Replaces the timer with the words "two" "one" when the countdown reaches 2, 1.
5. Checks the server for last-second button clicks when the countdown reaches 0, and:
a) Deactivates the button during this check.
b) Replaces the timer with the word "verifying" during this check.
6. If no last-second clicks are found:
a) Replace the timer with the words "game over".
b) The button remains deactivated.
c) Redirect the winner (the last user to click the button before 0) to another page to claim their prize.
7. Can accomodate as many separate and unique timer/button/<div> setups as I want on a single page.
<html>
<!--#include virtual="includes/servertime.cgi"-->
<script type=text/javascript language="javascript">
theServerTime = document.getElementById('servertime').value;
document.getElementById('showTimeHere').innerHTML = theServerTime;
</script>
<div id="showTimeHere"></div>
</html>
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)
Timestamp: Sun, 14 Mar 2010 21:22:55 UTC
Message: 'document.getElementById(...)' is null or not an object
Line: 7
Char: 1
Code: 0
#! /usr/bin/perl
print "Content-type:text/html\n\n";
$servertime = time;
print "<input type=hidden value='$servertime' id=servertime>";
<html>
<div id="showTimeHere">Server Time</div>
<!--#include virtual="includes/servertime.cgi"-->
<script type="text/javascript">
var theServerTime = document.getElementById('servertime').value;
document.getElementById('showTimeHere').innerHTML = theServerTime;
</script>
</html>