Jump to content

Timer Functions Only Work in Firefox


Niccaman

Recommended Posts

<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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. ;]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

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.