...

View Full Version : this function stops my clock working



Dan13071992
01-11-2012, 08:47 PM
I have a clock that works onbodyload, however using the below function on the same page, stops my clock even coming up, any help would be great.



<script language="JavaScript">
var HAS_EXPIRED = 'Time has Expired!';
var IS_NONE = 'None';
function secondCountdown(s){

if(s){

var timeleft = document.getElementById('timeleft').innerHTML;

if((timeleft == HAS_EXPIRED) || (timeleft == IS_NONE)) return false;

timeleft = timeleft.replace('<font>', '');
timeleft = timeleft.replace('</font>', '');

var time = timeleft.split(":");

var secs = time[2] * 1;
var mins = time[1] * 1;
var hrs = time[0] * 1;

secs += (mins * 60) + (hrs * 3600);
secs -= 1;

if(secs <= 0){

document.getElementById('timeleft').innerHTML = HAS_EXPIRED;
return false;

} else {

hrs = Math.floor(secs/3600);
secs -= (hrs * 3600);

mins = Math.floor(secs/60);
secs -= (mins * 60);

if(hrs < 10) hrs = '0' + hrs;
if(mins < 10) mins = '0' + mins;
if(secs < 10) secs = '0' + secs;

document.getElementById('timeleft').innerHTML = hrs + ':' + mins + ':' + secs;

}

}

setTimeout('secondCountdown(true)',1000);

}

bootloaderAdd('secondCountdown()');
bootloaderOn();

</script>

xelawho
01-11-2012, 09:41 PM
almost certainly you have (at least) two variables with the same name, but it is hard to tell really without seeing the clock function, too

Dan13071992
01-11-2012, 09:43 PM
sorry, i should have posted it,



function updateClock ( )
{

updateClock.tag=updateClock.tag|| document.getElementById("clock");
var m=" AM",
str=(new Date).toGMTString().match(/(\d{2}\:)+\d{2}/g)[0].replace(/^\d+/, function(a,b){
a*=1;
if(a>12){ a= (a-12)||12; m=" PM";}
return ("00"+a).slice(-2);
}) + m;

if(updateClock.last!=str){
updateClock.last=str;
updateClock.tag.firstChild.nodeValue = str;
}
}



also below is my body onload, please tell me if it is missing something:



<body onLoad="updateClock(); setInterval('updateClock()', 300 ); secondCountdown(s); ">

nomanic
01-11-2012, 11:44 PM
remove these 2 lines from your code -



bootloaderAdd('secondCountdown()');
bootloaderOn();


leave everything else as it is

Dan13071992
01-11-2012, 11:47 PM
that did bring back the clock, however it stopped the coundown, here is the bootloader that you told me to remove:



function bootloaderOn(){
window.onload = bootloaderGo;
}
function bootloaderGo(){
for(var i = 0;i < bootloaders.length;i++)
eval(bootloaders[i]);
}
function bootloaderAdd(func){
bootloaders[bootloaders.length] = func;
}
var bootloaders = new Array();

nomanic
01-12-2012, 01:01 AM
should be this -



<body onLoad="updateClock(); setInterval('updateClock()', 300 ); secondCountdown(); ">


the 's' was crashing it

Dan13071992
01-12-2012, 01:05 AM
if i leave out the


bootloaderAdd('secondCountdown()');
bootloaderOn();


the clock works, however if i put it back in the clock stops working and the countdown works again :S

nomanic
01-12-2012, 12:42 PM
did you change your body onload line?

Dan13071992
01-12-2012, 12:44 PM
yes i did

Dan13071992
01-14-2012, 09:42 PM
can anybody else figure this one out as im still having trouble :S

xelawho
01-14-2012, 10:18 PM
can you post your full code, and be a little more specific about what the problem is?

Dan13071992
01-15-2012, 12:37 AM
basically, when this script is inserted into the page, the clock on my website stops working, this is the inserted page called countDown.js:



var HAS_EXPIRED = 'Time has Expired!';
var IS_NONE = 'None';
function secondCountdown(s){

if(s){

var timeleft = document.getElementById('timeleft').innerHTML;

if((timeleft == HAS_EXPIRED) || (timeleft == IS_NONE)) return false;

timeleft = timeleft.replace('<font>', '');
timeleft = timeleft.replace('</font>', '');

var time = timeleft.split(":");

var secs = time[2] * 1;
var mins = time[1] * 1;
var hrs = time[0] * 1;

secs += (mins * 60) + (hrs * 3600);
secs -= 1;

if(secs <= 0){

document.getElementById('timeleft').innerHTML = HAS_EXPIRED;
return false;

} else {

hrs = Math.floor(secs/3600);
secs -= (hrs * 3600);

mins = Math.floor(secs/60);
secs -= (mins * 60);

if(hrs < 10) hrs = '0' + hrs;
if(mins < 10) mins = '0' + mins;
if(secs < 10) secs = '0' + secs;

document.getElementById('timeleft').innerHTML = hrs + ':' + mins + ':' + secs;

}

}

setTimeout('secondCountdown(true)',1000);

}

bootloaderAdd('secondCountdown()');
bootloaderOn();


then theres the bootloaderadd function from the bottom of that one which is this:



function bootloaderOn(){
window.onload = bootloaderGo;
}
function bootloaderGo(){
for(var i = 0;i < bootloaders.length;i++)
eval(bootloaders[i]);
}
function bootloaderAdd(func){
bootloaders[bootloaders.length] = func;
}
var bootloaders = new Array();


then on the actual page theres this peice of code:




<div id="timeleft"><?php echo "".maketime2($fetch2->banktime).""; ?></div>


now with the countDown.js page included on the page the JS clock stops working, if you would like the code for the clock let me know and ill post it for you?

xelawho
01-15-2012, 12:49 AM
just give us the whole shebang, html and all... or even a link if the page is live...

Dan13071992
01-15-2012, 12:51 AM
the page is live, but you would need to log in, i can supply a password for you and username if that would be easier?

xelawho
01-15-2012, 12:57 AM
ok - or why don't you set up a guest account so we can all log in?

Dan13071992
01-15-2012, 01:01 AM
http://crimewave.360-tactics.co.uk/index.php is the login,

login using:

username = codingforums
password = codingforums

url of problem page:

http://crimewave.360-tactics.co.uk/bank2.php

Dan13071992
01-15-2012, 01:42 AM
i have knuckled it down to the:



<script language='JavaScript' type='text/javascript' src='js/countDown.js'></script>


the script to this file is the same as the above countDown.js

i believe it is this one as when i take the above line out of my page, the clock starts working again and so does another javascript function that only allowes number in some of the textboxes.

xelawho
01-15-2012, 01:55 AM
seems to me that your bootloaderOn doesn't like your clock.

and your update clock function was a little weird (for my html anyway).

here's the two of them working together, at least. maybe this will help:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body onLoad="updateClock(); setInterval(updateClock, 300 ); ">
<div id="timeleft">10:20:31</div>
<div id="clock"></div>
<script language="JavaScript">

function bootloaderOn(){
window.onload = bootloaderGo;
}
function bootloaderGo(){
for(var i = 0;i < bootloaders.length;i++)
eval(bootloaders[i]);
}
function bootloaderAdd(func){
bootloaders[bootloaders.length] = func;
}
var bootloaders = new Array();

var HAS_EXPIRED = 'Time has Expired!';
var IS_NONE = 'None';


setInterval(secondCountdown, 1000 )



function secondCountdown(){

var timeleft = document.getElementById('timeleft').innerHTML;

if((timeleft == HAS_EXPIRED) || (timeleft == IS_NONE)) return false;

timeleft = timeleft.replace('<font>', '');
timeleft = timeleft.replace('</font>', '');

var time = timeleft.split(":");

var secs = time[2] * 1;
var mins = time[1] * 1;
var hrs = time[0] * 1;

secs += (mins * 60) + (hrs * 3600);

secs -=1;

if(secs <= 0){

document.getElementById('timeleft').innerHTML = HAS_EXPIRED;
return false;

} else {

hrs = Math.floor(secs/3600);
secs -= (hrs * 3600);

mins = Math.floor(secs/60);
secs -= (mins * 60);

if(hrs < 10) hrs = '0' + hrs;
if(mins < 10) mins = '0' + mins;
if(secs < 10) secs = '0' + secs;
document.getElementById('timeleft').innerHTML = hrs + ':' + mins + ':' + secs;

}
}

//bootloaderAdd('secondCountdown()');
bootloaderOn();

function updateClock ( )
{


var m=" AM",
str=(new Date).toGMTString().match(/(\d{2}\:)+\d{2}/g)[0].replace(/^\d+/, function(a,b){
a*=1;
if(a>12){ a= (a-12)||12; m=" PM";}
return ("00"+a).slice(-2);
}) + m;

if(updateClock.last!=str){
updateClock.last=str;
document.getElementById("clock").innerHTML = str;
}
}
</script>
</body>
</html>

Dan13071992
01-15-2012, 02:04 AM
i have copied all of that, and it still wont work :S word for word i have done it using copy and paste. but it still doesnt work together on the same page :S

xelawho
01-15-2012, 02:16 AM
sorry - wrong version :o

all you have to do is comment out the bootloaderOn(); line :thumbsup:

Dan13071992
01-15-2012, 02:23 AM
thank you :) seriously, thank you so much, it has been bugging me soo much lol, thank you for putting up with my noobish question, ive not got alot of experience in JS and its so hard to learn.

Once again, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum