/    Sign up×
Community /Pin to ProfileBookmark

[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>

[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Rufe0Apr 05.2011 — setTimeout(function(){GetCount(secondsPassed,iid)}, 1000);[/QUOTE]


setTimeout(function(){GetCount(secondsPassed-1,iid)}, 1000);
Copy linkTweet thisAlerts:
@KorApr 05.2011 —  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. [/QUOTE]
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".
    Copy linkTweet thisAlerts:
    @mottwscauthorApr 05.2011 — 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.
    <i>
    </i>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).
    Copy linkTweet thisAlerts:
    @mottwscauthorApr 05.2011 — The change Rufe0 suggested worked!

    Thanks to both of you for your assistance.
    ×

    Success!

    Help @mottwsc spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...