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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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>

 

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.