views:

2949

answers:

4

I was hoping this jQuery plug-in would work, but it didn't http://noteslog.com/post/how-to-fix-the-resize-event-in-ie

I added a comment to his site, but they're moderated, so you might not see it yet.

But anyhow, let me explain my desire. I want a "resize" type of event to be fired when the user either pauses his resize, and/or completes his resize, not while the user is actively dragging the browser's window resize handle. I have a fairly complex and time consuming OnResizeHandled function I need to run, but not run 100 times just because the user widened the window by 100px and the event was fired for ever pixel of movement. I guess a best bet would be to handle it once the user has completed the resize.

+1  A: 

How about setting a timeout for when the user pauses the resize? Reset the timeout when resizing occurs, or fire the resize event handler if the timeout expires.

Russ Cam
How do you know when the user pauses the resize? What is the exact syntax in javascript/jQuery to get this info that the user has paused?
You know when the resize is "paused" when the width and the height of the element have not changed for a specified period i.e. the span of the timeout you set.
Russ Cam
+1  A: 

Borrow some ideas from concurrency solutions to manage the flood of events coming from the browser.

For example, when you first get a resize event, set a flag to true indicating that the user is currently resizing. Set a timeout to call the actual resize event handler after 1 second. Then, whenever this flag is true, ignore the resize event. Then, in the actual handler, once everything is done and correct, set the flag back to false.

That way you only process the latest event once every second (or some other period of time depending on your requirements). If the user pauses in the middle of resizing, it will process. If the user finished, it will process.

This might not be suitable for you, but there are many other ways of using locks that might be more helpful.

Welbog
That's what the jQuery plugin does, but it fails to do it correctly.
I don't have enough knowledge to do it correctly myself. Hence why I'm asking for the knowledge here. :) BTW, I'm not after width, height, top, left. I'm after knowing when the resize event is complete (meaning the user let go of the browser's resize handle)
Oh wait, I got mixed up. Too many questions. Sorry.
Welbog
Well, this kind of thing is really easy to do, so don't get discouraged. Go here: http://www.w3schools.com/htmldom/met_win_settimeout.asp It will get you started on the SetTimeout function, which is really all you need in this situation. When you get a resize event, don't process it directly...
Welbog
(continued) ... only process the event after a certain period of time has elapsed. That way you don't process the event too often.
Welbog
A: 

Since it's built in jQuery, can't you bind some kind of onComplete event to the extension and pass a function that should be executed when it's called?

Edit:

When the user starts resizing the window, set an interval that watches the window's dimensions. If the dimensions haven't changed in a pre-set time period of your specification, then you can consider the window resizing "completed". Your javascript function of choice in this would be setInterval(), which accepts two arguments - a function to call each tick, and how long each tick takes in milliseconds.

As to how to write that specifically within the jquery framework, well, I don't know enough about jquery to be able to tell you that. Perhaps someone else can help you more specifically, but at the same time you may consider extending the jquery extension you already linked with an onComplete event that works like I just described.

Rahul
An onComplete event would be very nice. However, what is the javascript (or jQuery) syntax/command for this?
I don't know enough about jquery to provide you with a specific answer, but Russ Cam suggests approximately the same thing in his solution. I'll explain in an edit.
Rahul
+5  A: 

How about some code like this:

function resizeStuff() {
 //Time consuming resize stuff here
}
var TO = false;
$(window).resize(function(){
 if(TO !== false)
    clearTimeout(TO);
 TO = setTimeout(resizeStuff, 200); //200 is time in miliseconds
});

That should make sure the function only resizes when the user stops resizing.

Pim Jager
Yep. Spot on. Tried and tested.
Cheekysoft