473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

setTimeout(); function overloading/slowing down??

2
I have the following code which i use to fadeout 18 elements on mouseout, which works great on 1 element, but soon begins to slow down, or fail as I mouse over and off of the additional objects.... any idea how to improve the script?

Expand|Select|Wrap|Line Numbers
  1. var fademe = 0;
  2. var opac = 1;
  3. function hideObj(hideid) { 
  4. hideme=document.getElementById(hideid);
  5. hideme.style.visibility="hidden"; 
  6. hideme.style.opacity= 1;
  7. }
  8. function showObj(showid) { 
  9. showme=document.getElementById(showid);
  10. showme.style.opacity=1; 
  11. showme.style.visibility="visible"; 
  12. opac = 1;
  13. }
  14.  
  15.  
  16. function closeAll() {  
  17.     hideObj('colors01');
  18.     hideObj('colors02');
  19.     hideObj('colors03');
  20.     hideObj('colors04');
  21.     hideObj('colors05');
  22.     hideObj('colors06');
  23.     hideObj('colors07');
  24.     hideObj('colors08');
  25.     hideObj('colors09');
  26.     hideObj('colors10');
  27.     hideObj('colors11');
  28.     hideObj('colors12');
  29.     hideObj('colors13');
  30.     hideObj('colors14');
  31.     hideObj('colors15');
  32.     hideObj('colors16');
  33.     hideObj('colors17');
  34.     hideObj('colors18');
  35.     }
  36.  
  37.     function fadeObj(fadeid) 
  38. {
  39.  
  40.     fademe = document.getElementById(fadeid);
  41.     fademe.style.opacity = 1;
  42.  
  43.     setOpacity(1);
  44.     }
  45.  
  46.  
  47. function setOpacity (opac) 
  48. {
  49.  
  50.   if (opac>0) {
  51.     opac= fademe.style.opacity -= 0.05; 
  52.     var myinterval = setTimeout("setOpacity()",50);
  53. return;
  54.     } else {
  55.  
  56.  
  57.  
  58. clearTimeout(myinterval);
  59. return;
  60. }
  61.     return;
  62.  
  63.  
Feb 26 '07 #1
2 1928
Some ideas...

1. When setOpacity is called the first time as a timeout, it is not passed an opacity value:
Expand|Select|Wrap|Line Numbers
  1.   var myinterval = setTimeout("setOpacity()",50);
  2.  
I assume you intend for it to use the global variable opac. However, opac has local scope in setOpacity (it is a function parameter). You won't need the global opac if you do something like this:
Expand|Select|Wrap|Line Numbers
  1.   var myinterval = setTimeout("setOpacity(" + opac + ")",50);
  2.  
2. myinterval is a local variable and will not exist when clearTimeout is called. Also you would need a 'myinterval' for each element which is to be faded.

3. setOpacity only works on the element assigned to the global 'fademe' variable. When you call setOpacity 18 times, you kick off 18 timers. When each of those 18 timeout, they call setOpacity which uses the last value assigned to 'fademe' - which is 'colors18'.

setOpacity needs to be told which element to work on. There are many different ways, but here is one technique:
Expand|Select|Wrap|Line Numbers
  1. function setOpacity(id)
  2. {
  3.   var e = document.getElementById(id);
  4.   var o = +e.style.opacity - 0.05;
  5.   if (opac > 0) {
  6.     setTimeout("setOpacity('" + id + "')", 50);
  7.     e.style.opacity = o;
  8.   }
  9.   else {
  10.     e.style.opacity = 0;
  11.   }
  12. }
  13.  
Note that this no longer needs an opac argument, nor global vars, nor clearTimeout. Set the element's opacity before calling setOpacity (as in fadeObj).
Feb 26 '07 #2
phauwn
2
Thanks, once i sorted out the global and local variables properly, everything seemed to work a little better :)
Feb 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
6
by: Max | last post by:
Hi, I'm not exactly sure why this doesn't work. I'm basically just trying a simple approach at a slide down div. function slide_div { ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.