Jump to content

Slight Script Problem


RockinPurdy

Recommended Posts

Hey there, I have a script that I've made to countdown to the next 10 minute interval as shown below:

 

function TimeLeft() {
var Time = new Date();
var Minutes = Time.getMinutes();
var Seconds = Time.getSeconds();
var MinutesLeft = 0;
var SecondsLeft = 0;
if (Minutes < 10) {
	MinutesLeft = 10 - Minutes;
} else if (Minutes < 20) {
	MinutesLeft = 20 - Minutes;
} else if (Minutes < 30) {
	MinutesLeft = 30 - Minutes;
} else if (Minutes < 40) {
	MinutesLeft = 40 - Minutes;
} else if (Minutes < 50) {
	MinutesLeft = 50 - Minutes;
} else if (Minutes < 60) {
	MinutesLeft = 60 - Minutes;
}
SecondsLeft = 60 - Seconds;
if (SecondsLeft < 26) {
	MinutesLeft = MinutesLeft - 1;
	SecondsLeft = 60 - (26-SecondsLeft);
} else {
	SecondsLeft = SecondsLeft - 26;
}
if (SecondsLeft < 10) {
  		SecondsLeft = "0" + SecondsLeft;
  	}
document.getElementById('time').innerHTML="Share Value Updates in "+MinutesLeft+":"+SecondsLeft;
Timeout = setTimeout('TimeLeft()',500);
}

 

The problem is, it freezes when it gets to 1:00... and then when I refresh nothing comes up.. but then when I refresh some more times it will show 10:26 which it shouldn't.

 

Btw, the 26 is an offset.. ie. the countdown was 26 seconds off than what it should have been.

Link to comment
Share on other sites

as an fyi:i'm assuming that your getting your starting time from a server language(php?)... and its taking time before it hits the browser before it can start...(thats where the 26~ seconds comes from...

 

also... since it takes js some time to run through lines of code "Timeout = setTimeout('TimeLeft()',500);" will be HIGHLY inaccurate... if you were to use "setInterval()" it'd be FAR more accurate :-)

 

Link to comment
Share on other sites

function TimeLeft() {
var Time = new Date();
var Minutes = Time.getMinutes();
var Seconds = Time.getSeconds();
var MinutesLeft = 0;
var SecondsLeft = 0;
if (Minutes < 10) MinutesLeft = 10 - Minutes;
else if (Minutes < 20) MinutesLeft = 20 - Minutes;
else if (Minutes < 30) MinutesLeft = 30 - Minutes;
else if (Minutes < 40) MinutesLeft = 40 - Minutes;
else if (Minutes < 50) MinutesLeft = 50 - Minutes;
else if (Minutes < 60) MinutesLeft = 60 - Minutes;
SecondsLeft = 60 - Seconds;
if (SecondsLeft < 26) {
  MinutesLeft = MinutesLeft - 1;
  SecondsLeft = 60 - (26-SecondsLeft);
} else SecondsLeft = SecondsLeft - 26;
if (SecondsLeft < 10)  SecondsLeft = "0" + SecondsLeft;
document.getElementById('time').innerHTML="Share Value Updates in "+MinutesLeft+":"+SecondsLeft;
}
Timeout = setInterval('TimeLeft()',1000);

 

that way the function will loop every 1 second regardless of how long it takes to process the function...

 

still... javascript is based off of the computer's time... it'll still be a little bit wrong(depending on the computer it'll run a little faster/slower then intended)... it shouldnt be a dependant on the rest of your site... just an addon :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.