简体   繁体   English

从另一个函数onclick button2停止递归函数调用onclick button1

[英]stopping a recursive function call onclick button1 from a different function onclick button2

I'm trying to determine how to stop a recursive function call that came from a click to 'start_button' by calling a different function when 'stop_button' is clicked. 我试图通过单击“ stop_button”时调用另一个函数来确定如何停止从单击“ start_button”而来的递归函数调用。

  • User clicks 'start_button' and the slide down/up animation continues to loop "infinitely" until... 用户单击“开始按钮”,向下/向上滑动动画继续“无限”循环,直到...
  • User clicks 'stop_button' and the looped animations will stop. 用户单击“ stop_button”,循环的动画将停止。

I'd like to keep two different buttons instead of having one dual-purpose start/stop button. 我想保留两个不同的按钮,而不要使用一个双重用途的“开始/停止”按钮。 With my code below, the animation starts and loops, but does not stop when clicking the stop button. 使用下面的代码,动画将开始并循环播放,但单击“停止”按钮时不会停止。 I'd like for the stop button to stop the animation when clicked. 我想单击“停止”按钮来停止动画。

http://jsfiddle.net/ZdtmZ/ http://jsfiddle.net/ZdtmZ/

var stopSliding = false;

$('#stop_button').click(function(){
        stopSliding = true;
});

$('#start_button').click(function infiniteLoop(){
    if (stopSliding == true)
    {
        $('#top_message').stop();
        return;
    }       
    else
    {
        $('#top_message').hide().slideDown(2000);
        $('#top_message').slideUp(2000);    
        infiniteLoop();
        return;
    }
});

You are calling infiniteLoop synchronously, this means that it is impossible for any other Javascript to execute, like the click handler for your stop button. 您正在同步调用infiniteLoop,这意味着其他任何Javascript都无法执行,例如停止按钮的单击处理程序。 You need to make the call asynchronous so that your click handler can execute. 您需要使调用异步,以便点击处理程序可以执行。 You could use setTimeout(infiniteLoop, 0); 您可以使用setTimeout(infiniteLoop, 0); to do this, but you still won't get the behaviour you want, because you still aren't waiting for the slider to slide up before calling infiniteLoop. 这样做,但是您仍然不会得到想要的行为,因为在调用infiniteLoop之前,您还没有等待滑块向上滑动。 So instead you should pass infiniteLoop as a callback to slideUp. 因此,您应该将infiniteLoop作为对slideUp的回调传递。 That will make it asynchronous and make it wait for the animation to complete: 这将使其异步,并使其等待动画完成:

$('#top_message').slideUp(2000, infiniteLoop);

Your updated JSFiddle . 更新的JSFiddle

The problem is probably, that by the time you want to stop your animation, there are still numerous animations waiting in the queue to be executed (since your code runs way faster than the actual animations). 问题可能是,到您要停止动画时,仍然有许多动画在队列中等待执行(因为您的代码比实际动画运行得快得多)。 By providing the optional argument clearQueue of the stop function, you can delete all waiting animations: 通过提供stop函数的可选参数clearQueue ,可以删除所有等待的动画:

$('#top_message').stop(true)

I would write this like : 我会这样写:

$('#start_button').click( function() {
    if ( stopSliding ) return false;
    $('#top_message').hide().slideDown(2000, function() {
          $('#top_message').slideUp(2000, $('#start_button').click);
    });
    return false;
});

$('#stop_button').click( function() {
    stopSliding = true;
    $('#top_message').stop();
});

I just did reset the stopSliding variable to flase this way it does not stop animation if you click again on start animation . 我只是做了复位stopSliding变量flase这样,如果你在启动动画再次点击它不会停止动画。

jsfiddle 的jsfiddle

var stopSliding = false;

$('#stop_button').click(function(){
    stopSliding = true;
});

$('#slide_button').click(function infiniteLoop(){
    if (stopSliding == true)
    {
        $('#top_message').stop();
        stopSliding = false;
        return;
    }       
    else
    {
        $('#top_message').hide().slideDown(2000);
        $('#top_message').slideUp(2000, infiniteLoop);
        return;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM