/    Sign up×
Community /Pin to ProfileBookmark

Return to the start of my js script

Hello,

I am currently working on a script, and I cannot for the life of me figure out how to return to the start of the script and re-run it fresh in the same browers session – basically, it just keeps looping when the counter hits 0.

here is the script below. You will see I added an alert box to note the seconds have hit 0, but I cannot get the script to reset and restart back at

[CODE]mins = .1 [/CODE]

SOS! I do not need the alert box. I just put it there to test the location of the “flag” to reset.

[CODE]<html>
<head>
<title>Countdown</title>
<script type=”text/javascript”>
// how many minutes
var mins = .1;

// how many seconds (don’t change this)
var secs = mins * 60;
function countdown() {
setTimeout(‘Decrement()’,1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById(“minutes”);
seconds = document.getElementById(“seconds”);
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs–;
setTimeout(‘Decrement()’,1000);

if (secs == 0) {
alert(“please go back to the beggining!”);
}

}

}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
</script>
</head>
<body onLoad=”countdown();”>

<style type=”text/css”>

#countdownBox, #colon, input, #seconds, #minutes {
background-color: #CC0000;
color: #FFFF00;
font-size: 30px;
border: 0px;
font-weight: 900;
font-style: italic;
}

</style>

<div id=”countdownBox”>
<input id=”minutes” type=”text” style=”width: 55px;”><span id=”colon”>:</span>
<input id=”seconds” type=”text” style=”width: 55px;”>
</div>

</body>
</html>[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@McFlyauthorSep 20.2008 — I'm surprised know one knew this. I am pretty new to js, and I resolved it.

Is there are more advance js forum that anyone knows of? I might be in the wrong place ?
Copy linkTweet thisAlerts:
@WebJoelSep 20.2008 — You're brilliant, no doubt about it.
Copy linkTweet thisAlerts:
@McFlyauthorSep 20.2008 — I wouldn't consider myself brilliant. I just try hard.

Here is the solution that worked for me. Maybe there is a better way - probably so:

[CODE]location.reload();[/CODE]
Copy linkTweet thisAlerts:
@McFlyauthorSep 20.2008 — Well now I face a new problem. The darn counter won't show the numbers in IE 7.

Back to the grind...

Thanks for any tips or hints :-)
Copy linkTweet thisAlerts:
@McFlyauthorSep 21.2008 — Solved my second issue :-)

[B]minutesVar[/B] = document.getElementById("minutes");

[B]secondsVar[/B] = document.getElementById("seconds");

IE 7 does not like the id the same as the var name for the getelement object.
×

Success!

Help @McFly 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.25,
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,
)...