Which function to use for inserting string on a timer?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Which function to use for inserting string on a timer?

 
0
  #1
Apr 18th, 2007
I'm learning javascript. My first idea has led to a problem. I'm trying to set up strings to load at random intervals on different parts of the page, but I don't know the right function to use with setTimeout(). I hear "don't use document.write" and "probably don't use innerHTML", but are other functions outside of javascript? Please help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Which function to use for inserting string on a timer?

 
0
  #2
Apr 18th, 2007
For instance, why won't this code work?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // this is in the head, other functions working fine
  2. function addTextNode() {
  3. var newtext = document.createTextNode(" Some text added dynamically. ");
  4. var para = document.getElementById("p1");
  5. window.setTimeout("para.appendChild(newtext);", 1000);
  6. }
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. //this is in the body
  2. <div style="border: 1px solid red">
  3. <p id="p1">First line of paragraph.</p>
  4. </div><br />
  5.  
  6. <button onclick="addTextNode();">add another textNode</button>
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,081
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Which function to use for inserting string on a timer?

 
0
  #3
Apr 18th, 2007
setTimeout() can also take a function as the first parameter.

Using an anonymous function you can rewrite your JS code as:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // example of using a JS closure to persist a lexical environment/scope
  2. function addTextNode() {
  3. var newtext = document.createTextNode(" Some text added dynamically. ");
  4. var para = document.getElementById("p1");
  5. window.setTimeout(function() { para.appendChild(newtext); }, 1000);
  6. }

What happens is that setTimeout() is executed in the scope of the window (global scope). The variables "newtext" and "para" are private to the function addTextNode(), thus they only exist in the scope of that function.

When you however use a function instead of a string as the first parameter of setTimeout(), what happens is that JS will create a closure that makes "para" and "newtext" available in the scope of the anonymous function.

This is a valuable part of the JS language that will take care of scope issues for you instead of having you resort to using the global scope.

You could also use the global scope:

eg:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // example of using a JS closure to persist a lexical environment/scope
  2. function addTextNode() {
  3. newtext = document.createTextNode(" Some text added dynamically. ");
  4. para = document.getElementById("p1");
  5. window.setTimeout("para.appendChild(newtext);", 1000);
  6. }

but you will run into issues of overwriting the variables as they are available globally.

The closure is private to the function creating the closure however. So it cannot be overwritten from the global scope.

This is used a lot with the XMLHttpRequest object. And also with Event handlers, mostly unknowingly.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC