Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2003, 01:36 PM   #1 (permalink)
anon
Guest
 
Posts: n/a
Timers

So you went to a site and you saw that awesome clock that updated in Real Time? And you tried to make it or duplicate it but only got a static one that updated the time then stopped? Well here my friends, we are going to discuss timers so you can make that awesome Real Time clock or anything else that involves timers. First off you must have already elementary knowledge of JavaScript and must know what objects are and how to use them.

Long ago, when JavaScript was still young, you were only allowed to use one-shot timers, meaning they worked once and it was over! Well when people started to use them in loops more then by themselves, then a timer was created that ran in intervals. So now lets talk about both. To create a one-shot timer that ran after 10 seconds you would just have to run:
Code:
<script> 
window.setTimeout("alert('10 seconds have passed!')", 10000); 
</script>
here you see that the first paramater passed to setTimeout is what to do and the second is the time, it is in milliseonds, meaning to get 1 second you have to put in 1000. if you know the amount of seconds, just multiply it by 1000 and you have it. Now the setTimeout only releases one bit on info, it's timer ID. Meaning to stop the timer you would have to know the timer ID, for example:
Code:
<script> 
var ID = window.setTimeout("alert('haha 10 seconds!')", 10000); 
window.setTimeout("clearTimeout(ID);alert('timer stopped')", 3000) 
</script>
as you see we declare a variable called ID then we make it equal to the output of window.setTimeout. Then we make another one-shot timer that executes at 3 seconds, 7 seconds before the first and does clearTimeout(ID). clearTimeout(ID) stops the one-shot timer (that executes at 10 seconds). so now we know how to start and stop one-shot timers, we could loop them like this:

<script>
function timer() {
window.setTimeout("some_blah()", 1000);
}

function some_blah() {
alert("one second! Ha!");
timer()
}
</script>

This would loop it, but it is not convinient and not practical, so lets look at intervals. To make it so that it executes every 10 seconds and has a alert message saying "haha!" we would do:
Code:
<script> 
setInterval("alert('haha!')", 10000) 
</script>
This is alot like setTimeout right? Well yeah, but it's alot faster then looping it... And instead of clearTimeout we have clearInterval and they work the same. So you're thinking, yeah yeah yeah, shut up already and tell me how to make a real time clock. Ok here is a basic one that will show up at the bottom of the screen by using window.status and tells the user how long they have been on your site.
Code:
<html> 
<body onload="window_Onload()"> 
<script> 
var time=new Array(0,0,0) 


function window_Onload() 
{ 
setInterval("updateTime()",1000); 
} 

function updateTime() 
{ 
time[0]++ 
if (time[0] >= 61) 
{ 
time[1]++ 
time[0]=0 
} 
if (time[1] >= 61) 
{ 
time[2]++ 
time[1]=0 
} 

window.status=time[2] + " hours, " + time[1] + " minutes, and " + time[0] + " seconds." 
} 
</script> 
</body> 
</html>
As you see every second it runs updateTime() and seconds increase and window.status is updated... Now you could easily make this so it shows the time, in Real Time, something like this would work.

Code:
<html> 
<body onload="window_Onload()"> 
<script> 
var date; 
function window_Onload() 
{ 
setInterval("updateTime()",1000); 
} 

function updateTime() 
{ 
date=new Date() 
window.status=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds() 
} 
</script> 
</body> 
</html>
So now you got that awesome Real Time clock!

Now that you know how to use timers I am sure you can put this knowledge to good use.
  Reply With Quote
Old 04-20-2004, 02:57 PM   #2 (permalink)
gcollier
Registered User
 
Join Date: Apr 2004
Posts: 5
gcollier is on a distinguished road
Timers

I realize that "Timers" is not the burning issue in JavaScript today, but I've just spent two days looking thu the web for a countdown timer with the following features:

1. Counts down for a time-period (e.g. 3 or 5 days) where time-period could be set in the code, but NOT from or to a specific date
2. Format: 71:16:35 (hrs, mins, secs)
3. Starts automatically when window opens (no need for start or stop button)
4. Can be shown in 2 or more locations on the page synchronously -- e.g., top, middle, bottom of a long page.

I know some basic things about JavaScript, but this is over my head. Any help would be appreciated. There are 5 million scripts for counting down to a date. But I was not able to find any like I've described.

The purpose of such a timer would be for effect -- obviously, no one is going to leave a page open for 3-5 days! Here's the page I want to put it on. The timer on this page now is very nice, but I don't need all the buttons on it, and I don't know how to get them off without destroying the timer. It also has to be kickstarted with a start button, and viewers can change the numbers on the timer, which I don't want. Here's the page.

http://www.home-based-business-alive.com

Thanks for any help,
Gary
gcollier is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
countdown timers gcollier HTML, XML, Javascript, AJAX 6 04-28-2004 12:02 PM
Timers anon Lounge 1 01-06-2003 10:12 AM


All times are GMT -8. The time now is 10:37 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting