Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post

    Making text blink

    I'm building a multi-object Javascript blinker.

    Something's broken, but I can't tell what.

    Code:
    <div id="agent1">blah</div>
    <div id="agent2">bleh</div>
    
    <script language="javascript" type="text/javascript">
    		var BlinkToggle = new Array();
    		
    		BlinkToggle[1] = false;BlinkToggle[2] = true;
    		
    		function BlinkText(AgentNum,objID){
    			BlinkToggle[AgentNum] = !BlinkToggle[AgentNum];
    //document.write(BlinkToggle[AgentNum]);
    			BlinkObject=document.getElementById(objID);
    			if(BlinkToggle[AgentNum] == false)
    				{BlinkObject.style.visibility = 'hidden'}
    				else
    				{BlinkObject.style.visibility = 'visible'};
    			
    				
    			window.setTimeout(BlinkText(AgentNum,objID),3000);
    		}
    		
    		BlinkText(1,"agent1");
    		BlinkText(2,"agent2");
    </script>
    1. IE gives me an out of memory error on line 13.
    2. If I uncomment the doc.write command, it shows that the function appears to be looping, but obviously not once every 3 seconds.
    3. I've tried using setTimeout with single and double quotes, and without any at all, around the called function.

    Anyone tell me what's not working here?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Code:
    window.setTimeout(BlinkText(AgentNum,objID),3000);
    runs BlinkText immediately and then the function the code expects that call to return gets queued to run three seconds later.

    To have BlinkTest run after a three second delay you need to code it as:

    Code:
    window.setTimeout(function() {BlinkText(AgentNum,objID)},3000);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    All I'm going to say is that <blink> was deprecated for a reason.

    http://htmlcss.wikia.com/wiki/Blink
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,725
    Thanks
    6
    Thanked 1,013 Times in 986 Posts

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    It should be.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,185
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    In any case, the code is horribly over-complex and could and should be simplified:
    Code:
    <html>
    <body>
    <div id="agent1">blah</div>
    <div id="agent2">bleh</div>
    
    <script type="text/javascript">
    (
      function()
      {
         function blink( )
         {
              for ( var b = 1; b < 9999; ++b )
              {
                  var div = document.getElementById("agent" + b )
                  if ( div == null ) return;
      
                  div.style.visibility = ( div.style.visibility == "hidden" ) ? "visible" : "hidden";
              }
          }
    
          setInterval( blink, 3000 );
      }
    )();
    </script>
    </body>
    </html>
    Now it works with any number of "agentNN" divs (or spans or whatever) *and* with any initial visibility per agentNN (provided the style is code inline).

    That is, you could add
    Code:
        <span id="agent3" style="visibility: hidden;">wowser!</span>
    And it would autmatically be blinked, as well, but in opposition to the blinks for agent1 and agent2, as given.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    May 2007
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Quote Originally Posted by felgall View Post
    Code:
    window.setTimeout(BlinkText(AgentNum,objID),3000);
    runs BlinkText immediately and then the function the code expects that call to return gets queued to run three seconds later.

    To have BlinkTest run after a three second delay you need to code it as:

    Code:
    window.setTimeout(function() {BlinkText(AgentNum,objID)},3000);
    It's been a while since I coded in JavaScript. Thank you for reminding me about the function().

    Quote Originally Posted by WolfShade View Post
    All I'm going to say is that <blink> was deprecated for a reason.
    Ah, the 90's ... full of <blink>, glitz, and badly formatted websites! Yep, I know blink is depreciated, but this is for a work project and parts of it need to blink when certain time thresholds have been reached.

    Quote Originally Posted by Old Pedant View Post
    In any case, the code is horribly over-complex and could and should be simplified:
    Code:
    <html>
    <body>
    <div id="agent1">blah</div>
    <div id="agent2">bleh</div>
    
    <script type="text/javascript">
    (
      function()
      {
         function blink( )
         {
              for ( var b = 1; b < 9999; ++b )
              {
                  var div = document.getElementById("agent" + b )
                  if ( div == null ) return;
      
                  div.style.visibility = ( div.style.visibility == "hidden" ) ? "visible" : "hidden";
              }
          }
    
          setInterval( blink, 3000 );
      }
    )();
    </script>
    </body>
    </html>
    Now it works with any number of "agentNN" divs (or spans or whatever) *and* with any initial visibility per agentNN (provided the style is code inline).

    That is, you could add
    Code:
        <span id="agent3" style="visibility: hidden;">wowser!</span>
    And it would autmatically be blinked, as well, but in opposition to the blinks for agent1 and agent2, as given.
    I see what you did there. Thank you.


  •  

    Posting Permissions

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