简体   繁体   English

如何阻止javascript函数调用直到完成

[英]how to block javascript function from invoking till completed

how to block click events to invoke function till this function completed , i have written this function : 如何阻止单击事件以调用函数,直到该函数完成为止,我已经编写了此函数:

 $(".cycle-next").on('click',function(){ var contWidth = $(".container").width(); var finalWidth = ((contWidth*.25)*-1)+(parseInt($(".inner").css("left"))); var contWidthC = contWidth*-1; if(finalWidth === contWidthC){ $(".inner").animate({left:"0"},200,function(){ }); } }); 

here once we click on (cycle-next) element , the function start executing , suppose the controller in the middle of function , at this point user again click on the element for second time , again the controller start from the beginning of function without completing , How to make second click wait until completing ? 在这里,一旦我们单击(cycle-next)元素,函数便开始执行,假设控制器位于函数中间,此时用户再次单击该元素,控制器再次从函数的开头开始而不完成,如何使第二次点击等待完成?

Using .promise() and $.when() (and an IIFE) should do the trick 使用.promise()$.when() .promise() (以及IIFE)应该可以解决问题

;(function() {
    var p = $.when(); // initial "promise" to get things moving on the first click
    $(".cycle-next").on('click',function(){
        p = p.then(function() { // chain the promise
            var $inner = $('.inner');
            var contWidth = $(".container").width(); 
            var finalWidth = ((contWidth * .25) * -1) + parseInt($(".inner").css("left")); 
            var contWidthC = contWidth * -1;

            if (finalWidth === contWidthC) {
                return $inner.animate({ left: "0" }, 200, function() {
                    // put logic to execute when the animation ends here, or remove this if not needed
                }).promise(); // returns a promise that presumably resolves when animation completes
            }
        });
    });
})();

To be honest though, re-reading the question, 老实说,重新阅读问题,

  1. you start by saying you want to block subsequent clicks, 您首先说要阻止后续点击,
  2. you end the question saying that subsequent clicks should wait for the animation to complete 您结束了一个问题,说随后的点击应该等待动画完成

the two are mutually exclusive 两者是互斥的

This answer will, more or less, queue the click events to be processed sequentially once the previous animation completes 一旦上一个动画完成,此答案将或多或少地将要依次处理的点击事件排队

You can simply set a flag. 您可以简单地设置一个标志。 I chose a css class so you can style the buttonwhile it is disabled. 我选择了一个CSS类,以便可以在禁用按钮时为其设置样式。

if .cycle-next is a form-element, you could go with .prop("disabled", true) , or with a dataset-value .data("disabled", true) 如果.cycle-next是一个表单元素,则可以使用.prop("disabled", true)或数据集值.data("disabled", true)

$(".cycle-next").on('click',function(){
    var $this = $(this);
    if($this.hasClass("disabled")) return false;

    var contWidth = $(".container").width(); 
    var finalWidth = ((contWidth*.25)*-1)+(parseInt($(".inner").css("left"))); 
    var contWidthC = contWidth*-1;

    if(finalWidth === contWidthC){
        $this.addClass("disabled");
        $(".inner").animate({left:"0"},200,function(){
            $this.removeClass("disabled");
        });
    }
});

Try to use an AJAX call on click to disable the button and on success of the AJAX call enable the button again. 尝试在单击时使用AJAX调用以禁用该按钮,并在AJAX调用成功后再次启用该按钮。 This is a consistent solution 这是一个一致的解决方案

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

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