www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] timer script does not advance timers

  1. #1
    Join Date
    Aug 2008
    Posts
    8

    resolved [RESOLVED] timer script does not advance timers

    I'm using javascript to create timers that will start a count down after the page is loaded. I need to do the calculation of seconds for the timers in php on the page that calls this page with the javascript. I then pass the seconds when I write the javascript using php. Here I'm just showing the javascript with the seconds hard-coded for each timer. I've tried testing this as is (with the javascript hard-coded just as shown here), but all it will do is display the starting point for each timer but won't advance beyond that. It is supposed to show the timers counting down each second.

    Thanks for any suggestions.

    Code:
    <script type='text/javascript'>
    
    function GetCount(secondsPassed, iid){
    
    	amount = secondsPassed;
    	if(amount < 0){
    		document.getElementById(iid).innerHTML='Now!';
    	}
    	else{
    		days=0;hours=0;mins=0;secs=0;out='';
    
    		days=Math.floor(amount/86400);			
    		amount=amount%86400;
    
    		hours=Math.floor(amount/3600);			
    		amount=amount%3600;
    
    		mins=Math.floor(amount/60);				
    		amount=amount%60;
    
    		secs=Math.floor(amount);				
    
    		if(days != 0){out += days +' d ';}
    		if(days != 0 || hours != 0){out += hours +' h ';}
    		if(days != 0 || hours != 0 || mins != 0){out += mins +' m ';}
    		out += secs +' s';
    		document.getElementById(iid).innerHTML=out;
    
    		setTimeout(function(){GetCount(secondsPassed,iid)}, 1000);
    	}
    }
    
    window.onload=function(){
    	GetCount('120', 'countbox1');
    	GetCount('60', 'countbox2');
    	GetCount('30', 'countbox3');
    };
    
    </script>
    
    <div id="countbox1"></div>
    <div id="countbox2"></div>
    <div id="countbox3"></div>

  2. #2
    Join Date
    Apr 2009
    Posts
    96
    setTimeout(function(){GetCount(secondsPassed,iid)}, 1000);

    setTimeout(function(){GetCount(secondsPassed-1,iid)}, 1000);

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by mottwsc View Post
    I need to do the calculation of seconds for the timers in php on the page that calls this page with the javascript. I then pass the seconds when I write the javascript using php.
    Ha? How's that? Sounds confusing. Do you?:

    - have a time variable written with php in document A,
    - open with javascript the document B
    - pass that variable time from document A to document B
    - use that variable as a start time for a JavaScript counter
    - pass back this variable to the document A (when?)

    is this what is all about?

    I don't understand what is that "php variable".

  4. #4
    Join Date
    Aug 2008
    Posts
    8
    I will try what Rufe0 suggested later today, but here is what you were asking about Kor:

    first, to make it clear, doc A has the php server logic and doc B renders the html and js. doc b is an include file in doc a.

    Do I:
    - have a time variable written with php in document A,
    -> yes, that is the number of seconds
    - open with javascript the document B
    -> doc B is a php doc where I write the js dynamically so that I can fill in the seconds from doc A
    - pass that variable time from document A to document B
    -> the prior answer is how I pass the seconds from doc A to Doc B
    - use that variable as a start time for a JavaScript counter
    -> where you see the hard-coded seconds in the function (120, 60, 30) is where I will write the three different variables from doc A. I have only hard-coded them here to get this script working.
    Code:
    window.onload=function(){
    	GetCount('120', 'countbox1');
    	GetCount('60', 'countbox2');
    	GetCount('30', 'countbox3');
    };
    - pass back this variable to the document A (when?)
    -> I do not pass the variable back to doc A. the js keeps counting down the timers in doc B. I use a meta tag in doc B to refresh every 60 seconds. When that happens, doc a reruns the logic to calculate the three variables holding the seconds and that is used to reset the timer display and countdowns on doc b. Remember that I need to rely solely on the clock on the server (which is where doc a pulls its times to do the seconds calculations until the next event).

  5. #5
    Join Date
    Aug 2008
    Posts
    8

    Solved

    The change Rufe0 suggested worked!

    Thanks to both of you for your assistance.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles