/    Sign up×
Community /Pin to ProfileBookmark

setTimeout() not working as expected…

Here is my code. I am simply trying to count down from a specified number of seconds, which is loaded from a db using php. The problem is that it shows the amount of time, but doesn’t count down. I have looked over at least two dozen scripts, and the only difference is that I am passing a variable to it in seconds, not a timestamp or something that uses the date() function. Anyway, please have a quick look, and help a fellow out… Thanks, so very much…

This is in my head section…

[CODE]var secondstoadd = 0;
var secondsremaining = 0;

function timer(secondstoadd)
{
secondsremaining = secondstoadd;
if (secondsremaining < 0)
{
return;
}

var seconds = Math.floor(secondsremaining % 60);
secondsremaining = secondsremaining/60;
var minutes = Math.floor(secondsremaining % 60);
secondsremaining = secondsremaining/60;
var hours = Math.floor(secondsremaining % 24);
secondsremaining = secondsremaining/24;
var days = Math.floor(secondsremaining);

var outStr = “”;

if (seconds<=9) { seconds = “0” + seconds; }
if (minutes<=9) { minutes = “0” + minutes; }
if (hours<=9) { hours = “0” + hours; }

var outStr = days + ‘:’ + hours + ‘:’ + minutes + ‘:’ + seconds;

document.getElementById(‘countdown’).innerHTML=outStr;

secondsremaining = (secondsremaining – 1);
window.setTimeout(‘timer(‘+ secondsremaining + ‘)’);
}[/CODE]

And This is my call to the function…

[CODE]<td><div id=’countdown’><script type=”text/javascript”>new timer(” . $row[‘arrivaltime’]. “);</script></div></td>[/CODE]

Where $row[‘arrivaltime’] is a php var that populates the html with the number of seconds until you would arrive at your destination…

Thanks again.

Savaal

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Mar 14.2008 — your settimeout is missing the second parameter of how often you want it to run, but you also need a clearTimeout as well or else you will have an infinite loop.

Look at this
×

Success!

Help @Savaal 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...