Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用stop()加速Jquery delay()_Jquery - Fatal编程技术网

使用stop()加速Jquery delay()

使用stop()加速Jquery delay(),jquery,Jquery,下面是我的问题: 我有三个按钮: <button>Button A</button> <button>Button B</button> <button>Button C</button> 因此,如果按钮B没有返回任何数据并显示警报,并且我快速单击按钮C,按钮C确实有数据,则错误警报将一直保留,直到延迟完成,这不是预期的 我尝试将隐藏函数添加到else块: if ( no_data ) { $error.show

下面是我的问题:

我有三个按钮:

<button>Button A</button>
<button>Button B</button>
<button>Button C</button>
因此,如果按钮B没有返回任何数据并显示警报,并且我快速单击按钮C,按钮C确实有数据,则错误警报将一直保留,直到延迟完成,这不是预期的

我尝试将隐藏函数添加到else块:

if ( no_data )
{
    $error.show().delay(6000).fadeOut("slow");
}
else 
{
    $error.hide(); // we don't want the alert to keep showing

    // we have data, do some stuff
}
很好,现在如果查询成功,将删除显示的警报。但是,如果(如上所述)我单击按钮B,得到一个错误(警报被触发),然后单击按钮C,警报被删除。。。然后我再次(快速)点击按钮B,之前的延迟仍然在动画队列中,并且几乎立即删除警报

有没有这样一种方法:

if ( no_data )
{
    $error.show().delay(6000).fadeOut("slow");
}
else 
{
    // kill the previous fadeOut, then...

    $error.hide();

    // do other stuff
}
我已尝试添加停止功能:

$error.stop(true, true).hide();
但它似乎仍然不起作用。我假设这是因为延迟导致队列中没有动画,所以stop函数实际上从未删除任何内容


我做错了什么?

您是否尝试过使用dequeue()来中断delay()函数

您可以尝试以下方法:

 if ( no_data ){
   $error.show().delay(6000).fadeOut("slow");
 }else{
   // kill the previous fadeOut, then...

   $error.dequeue();

   // do other stuff
 }
 $c.on('click', function(e) {
   $x.hide();
   $x.dequeue();
 });
在您的代码中,它将如下所示:

 if ( no_data ){
   $error.show().delay(6000).fadeOut("slow");
 }else{
   // kill the previous fadeOut, then...

   $error.dequeue();

   // do other stuff
 }
 $c.on('click', function(e) {
   $x.hide();
   $x.dequeue();
 });
也就是说,我认为最好有一个状态机,您可以跟踪显示的错误状态,并且只在错误消失事件完成时启用按钮a、B和C。当未显示错误时,您可以启用按钮,但当检测到错误时,您可以使用setTimeout函数在6000ms时间内禁用按钮。

setTimeout()和clearTimeout()可以解决此问题:

    var $a = $('#btn-a');
    var $b = $('#btn-b');
    var $c = $('#btn-c');
    var $x = $('#alert');
    var timerHandle;

    $a.on('click', function (e) {
        $x.show();
        timerHandle = setTimeout(function () {
            $x.fadeOut("slow");;
        }, 6000);
    });

    $b.on('click', function (e) {
        $x.show();
        timerHandle = setTimeout(function () {
            $x.fadeOut("slow");;
        }, 6000);
    });

    $c.on('click', function (e) {
        $x.hide();
        clearTimeout(timerHandle);
    });

在清除错误或显示新错误之前,禁用按钮怎么样?我不希望用户必须支付等待罚款。对于阅读本文的其他人,请注意,队列清除和
stop()
delay()
不起作用。一开始我不知道确切的问题,这解释了为什么我找不到关于它的任何问题:)