alexandernst
alexandernst

Reputation: 15099

Callbacks overlapping

I have a simple resizable div (achieved with jQuery's resizable) and I'm handling the resize event with a function.

So, let's say that's my code:

var a = ...
$(div).resizable({ 
    options go here...

    resize: function(e, ui){
        make some operations with a
        ...
        ...
        keep making operations with a
        ...
        ...
        ok, we're done
    }
})

Now, let's say the resize callback is called a lot of times in a short period of time. How will javascript handle that? Will callbacks overlap the use of "a"? (Note that "a" is a global var!). Am I safe with this code or there might be a conflict of some kind?

Regards

Upvotes: 0

Views: 245

Answers (2)

Richard Connamacher
Richard Connamacher

Reputation: 3216

If you're asking about thread safety, JavaScript is single-threaded. A function like that can only be run one at a time, regardless of how often it's called.

So you should be safe.

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

JavaScript is single-threaded so the function calls can't "overlap". If you trigger the event multiple times then the function is executed entirely (minus any asynchronous functions it may call) in response to the first triggering, before it's executed again to handle the subsequent triggers.

However, changes to a in one call will still have been made when the function is executed again, so if it relies on the value of a you may run into problems - it comes down to exactly what that function is doing with the variable.

Upvotes: 1

Related Questions