-2

I have three buttons, each of which calls two functions jquery,one for display and one for close, pictures.

Inside the function to open, I have a list of sequential functions. However, it may happen that the User click a button and a function has not completed the list and click on it nother button.

So I need to call a function and interrupt another, anyone know how to do?

I am using jquery animate

Giovane Bueno
  • 23
  • 1
  • 9

1 Answers1

1

So I need to call a function and interrupt another, anyone know how to do?

You can't literally do that, because JavaScript on browsers is single-threaded (barring the use of Web Workers, but you only have on UI thread even then). So you can't interrupt the execution of a function call.

But if you're using jQuery's animation/effects functions, the calls you make just put actions in a queue that jQuery then runs asynchronously, and you can interrupt that, by calling stop on the element.

So by way of example:

function kickThingsOff() {
    // These could all be written in one line as a chain, but I'm doing them
    // separately for emphasis
    var elm = $("#myElement");
    elm.fadeOut("slow");
    elm.fadeIn("slow");
    elm.slideUp("slow");
}

function interruptIt() {
    $("#myElement").stop(true, true);
}

You can't actually interrupt kickThingsOff in the middle (between, say, the fadeIn line and the slideUp line), but you can interrupt the animation it queues up via stop.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I'm using animations images, but image is not fixed but several images over a background, an image arrives and another comes. I will using "stop". But if anyone keep clicking several times on the buttons, the javascript will end up, losing himself. Anyway thanks for the clarification! – Giovane Bueno May 06 '13 at 13:42