Auto-incrementing number...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
TS_Death_Angel
Forum Commoner
Posts: 31
Joined: Sat Dec 31, 2005 8:49 am

Auto-incrementing number...

Post by TS_Death_Angel »

Could someone lend me a hand? I have zero JS experience, but hopefully this is simple enough for someone to help me with it :P Say I output a number via PHP and want a specific number (also outputted by PHP) to be added every second. Would anyone be so kind to write a quick snippet to do this? ^^

Cheers!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

I'm not clear on what you want to do. You mention PHP, then you mention javascript. Once the page has been sent to the browser, PHP is out of the picture, it's a server side language. If you want a number to be displayed on the web page and continue to increment while the viewer is viewing it, it has to be done in a client side language, i.e., javascript. Writing a javascript snippet to do that is simple enough, but you need to tell us what else you are going to do or nobody can offer a script. What stops the incrementing? Does it go on as long as the page is viewed? What would you want to do with the incremented value? That would all need to be in the script logic.
TS_Death_Angel
Forum Commoner
Posts: 31
Joined: Sat Dec 31, 2005 8:49 am

Post by TS_Death_Angel »

Alright, the script just calculates my age to the nearest second. Here's the script I've got:

Code: Select all

<?php
					  
#
# On the fly age calculation
#
$dob = 689425200;
$now = time();
$dif = $now-$dob;

$yr  = 60*60*24*365;
$yrs = $dif/$yr;

$inc_sec = (($now+1-$dob)-1)/$dif; // how much to add per second
					  
echo $yrs;
					  
					  
?>
$inc_sec contains how much is to be added every second, while $yrs is my current age as calculated by the page reload. Once the page is loaded, I want the JavaScript code to take $yrs and add $inc_sec to it every second. Do you understand now? ^^'

EDIT: Yes, it should go on forever.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Okay, see that makes a huge difference: you can't possibly do that with a server side language. You could do it with Javascript. Check out this Javascript code for displaying a continuously updating clock (every second) on your screen. All you need to do is adapt it to manipulate the date-of-birth and convert to seconds:

http://www.w3schools.com/js/tryit.asp?f ... ming_clock

Have fun.
TS_Death_Angel
Forum Commoner
Posts: 31
Joined: Sat Dec 31, 2005 8:49 am

Post by TS_Death_Angel »

califdon wrote:Okay, see that makes a huge difference: you can't possibly do that with a server side language. You could do it with Javascript. Check out this Javascript code for displaying a continuously updating clock (every second) on your screen. All you need to do is adapt it to manipulate the date-of-birth and convert to seconds:

http://www.w3schools.com/js/tryit.asp?f ... ming_clock

Have fun.
Thanks :D It works now.
Post Reply