Niccaman Posted April 30, 2009 Share Posted April 30, 2009 <script language="javascript" type="application/javascript"> function timer() { var doc = document.getElementsByName('timeLeft'); for (var i=0;i<doc.length;i++) { if (doc[i].innerHTML == "Ready") {}else { doc[i].innerHTML -= 1; if(doc[i].innerHTML <= 0) { doc[i].innerHTML = "Ready"; } } }setTimeout("timer()",1000); } </script> <body onLoad="timer();"> ...and varying amounts of : <span name="timeLeft"></span>" around the page. Sometimes 1. None of these options seem to be working in IE, but always work in Firefox. Please help fix this. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 I think document.getElementsByName is Firefox restricted. I can definitely be wrong. I just looked it up briefly and a site stated that. I may have overlooked. Can you use something like document.getElementById() instead? Quote Link to comment Share on other sites More sharing options...
Niccaman Posted April 30, 2009 Author Share Posted April 30, 2009 You seem to be right, however i changed to this format to alow for multiple instances of timers on the page. I tested the code but made only 1 instance of: <span id="timeLeft"></span> changing the javascript to: function timer() { var doc = document.getElementById('timeLeft'); if (doc.innerHTML == "Ready") {}else { doc.innerHTML -= 1; if(doc.innerHTML <= 0) { doc.innerHTML = "Ready"; } }setTimeout("timer()",1000); } This worked for the one instance, proving your theory somewhat correct, however taking the apporach with for loop and "i"s seems to fail. any idea how to solve this? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 Well a for loop runs once. After it's run, it won't run again. For example: <div id="e"></div> <script type="text/javascript"> var e = document.getElementById("e"); for (var i = 0; i < 10; i++) { e.innerHTML = i; } </script> That runs once, and at the end, <div id="e"></div> will hold the value 9. But if you want to run it again, you need either setTimeout or setInterval. Get it? In your case, setInterval is better so you don't have to include the setTimeout inside the function. ;] Quote Link to comment Share on other sites More sharing options...
Niccaman Posted April 30, 2009 Author Share Posted April 30, 2009 Ok thanks for trying to help, i might explore that later, but my priority is to try and access potentially multiple elements (can change from 1 to more than 1 to 0) working in both IE and Firefox. It has been made aware that i cannot use getelementsbyname in IE. So how else can i do it? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 What's wrong with getElementById()? Try this: <div id="timer1">1000</div> <div id="timer2">5000</div> <script type="text/javascript"> function timer (dom) { if(dom.innerHTML <= 0) dom.innerHTML = "Ready"; if (dom.innerHTML != "Ready") { dom.innerHTML = parseInt(dom.innerHTML) - 1; } } </script> Does that trigger any thoughts? Quote Link to comment Share on other sites More sharing options...
Niccaman Posted April 30, 2009 Author Share Posted April 30, 2009 Sorry, i dont understand your code. Whats all the "dom" and and how are the elements being called? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 dom is a DOM element. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.