Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 09:18 PM   PM User | #1
SlowCoder
New Coder

 
Join Date: May 2007
Posts: 46
Thanks: 5
Thanked 1 Time in 1 Post
SlowCoder is an unknown quantity at this point
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?
SlowCoder is offline   Reply With Quote
Old 12-03-2012, 09:23 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-03-2012, 09:36 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 12-03-2012, 09:57 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,601
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
But text-decoration: blink is not.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 12-03-2012, 10:20 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 12-03-2012, 11:06 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-04-2012, 12:32 PM   PM User | #7
SlowCoder
New Coder

 
Join Date: May 2007
Posts: 46
Thanks: 5
Thanked 1 Time in 1 Post
SlowCoder is an unknown quantity at this point
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.
SlowCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.