views:

44

answers:

2

I'm trying to resize an html element (flash object) but it doesn't seem to respond more than once per second?

Is this a limitation imposed by browsers (both IE7 and FF3 do this)? Or should I be attempting to resize in a different/more efficient way?

function setHeightNow(height) { 

    if (document.getElementById) { 
     if (height > 0) { 
      var scaleItem = document.getElementById('application'); 
      scaleItem.style.height = height + 'px'; 
           }
    } 
   } 
A: 

If you are calling this function in a loop, as bobince mentioned in his/her comment, you should change it to a series of setTimeout calls (or setInterval) to give control back to the browser.

Something like this-

var i = INITIAL_VALUE;

(function() {
    setHeightNow(foo);
    if (i < FINAL_VALUE) {
        i++;
        setTimeout(arguments.callee, 0); //you can play around with the timeout.
    }
})();

Also

  1. The documents.getElementById check is kind of useless because all browsers support it.
  2. It would be wise to somehow take the document.getElementById call outside this repeating function if possible.
Chetan Sastry
OP UPDATE:This is only called once via a flash player ExternalInterface.call(), not currently in a loop of any kind. The issue appears if I click more frequently than ~1 click/sec. From my tests, the ExternalInterface.call() can reach the setHeightNow() method as often as possible, but "scaleItem.style.height = height + 'px';" isn't functioning.Is it possible that the element I'm attempting to change, is locked or still being updated by the browser's rendering engine? Is there a way to see if it is "ready" ?
James Lyon
A: 

It's certainly not a defined limitation; we run an animation loop that is triggered 30 times/sec. (Using a 33ms timeout.) Mostly we move backgrounds around (animations) or adjust opacity (fade in/out) but sometimes we also re-size elements.

However, all of those elements are absolutely positioned, or in a fixed container, so it doesn't trigger a re-layout by the browser. I suspect your problem is simply the cost of performing that re-layout, most of which would be down to the flash object itself.