Page 1 of 1

Auto-incrementing number...

Posted: Sat Dec 02, 2006 6:13 pm
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!

Posted: Sat Dec 02, 2006 6:44 pm
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.

Posted: Sat Dec 02, 2006 6:50 pm
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.

Posted: Sat Dec 02, 2006 10:05 pm
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.

Posted: Sun Dec 03, 2006 7:45 am
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.