Jump to content

[SOLVED] setTimeout not passing on argument


longtone

Recommended Posts

I am trying out this very simple script and getting errors.

 

function newtext(o)
{

  o.innerHTML="Hey! I have changed!";
  setTimeout("moretext(o)",1000);
}
function moretext(o)
{
  o.innerHTML="I just change with the time!";
}

The first line works fine, but after 1 second i get the error:

" o is not defined"

 

If I replace o with it's value in the second function:

 

function moretext(o)
{
  document.getElementById('id').innerHTML="I just change with the time!";
}

 

It works.

 

I'm confused ???

You're only showing us the functions. You aren't showing us how they are called. We can't tell you why they aren't working if you don't show us how they are called.

 

<div id = "id">

<p>abc</p>

</div>

<div>

<p><a href="#" onclick="newtext(document.getElementById('id')); return false" >def</a>

</p>

</div>

 

Unfortunately, you can't pass references to elements like that, which is where your problem is coming from. Or at least you can't pass them twice - it seems to work for the first time. But the problem is easy to fix - use this:

 

function newtext(eid)
{
  var o = document.getElementById(eid)
  o.innerHTML="Hey! I have changed!";
  setTimeout("moretext(eid)",1000);
}
function moretext(eid)
{
  var o = document. getElementById(eid)
  o.innerHTML="I just change with the time!";
}

 

Then call it with this:

 

<a href="#" onclick="newtext('id'); return false" >def</a>

 

Archived

This topic is now archived and is closed to further replies.

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