SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: type writter

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    newyork
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    type writter

    im trying to make a windows status bar type writter thingy that will display a message one letter at a time in say 1 second intervals.

    i keep getting "i is undefined" errors. can anyone help? im pretty new at JS so im sure my code has lots of bugs... any help would be greatly apprecaited...

    <script>
    function typeWritter()
    {
    var text = "text to be displayed";
    var temp = "";
    var i = 0;

    for (var i = 0; i <= text.length; i++)
    {
    self.setTimeout("display(i)", 1000);
    i++;
    }

    function display(i)
    {
    temp = temp + text.charAt(i);
    window.status = temp;
    }
    }
    </script>
    Last edited by squishyx; Aug 12, 2004 at 15:47.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Code:
    <script>
    function typeWritter() { //typewriter (one t)
      var text = "text to be displayed"; //local to this function, should be global
      var temp = ""; //should be global, but you don't really need it
      var i = 0; //useless
      
    //this won't work because you're setting n timeouts, almost all at once, so they'll 
    //timeout and execute all at once (n = text.length)  
      for (var i = 0; i <= text.length; i++) {
        // the string "display(i)" is literally display(i), not display( current_value_of_i )
        // as you expect - you need to use "display(" + i + ")"
        self.setTimeout("display(i)", 1000);
        i++;//you aleady incremented i in the for...
      }
      
      //this function is inside typeWriter...should be outside
      function display(i) {
    //this could work if temp was global and i was 
    //actually an int (as above)    
        temp = temp + text.charAt(i); 
        
        window.status = temp;// this line is okay
      }
    }
    </script>
    
    
    <script>
    //the fix - set body onload = typewriter(0)
    var text = "text to be displayed";
    function typewriter( i ) {
      window.status += text.charAt( i );
      if( i < window.status.length ) {
        setTimeout( "typewriter(" + ++i + ")", 100 );
      }
    }
    </script>
    

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    newyork
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx alot for your help! i see i need alot of tinkering....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •