jquery - Best way to prevent a javascript function from executing while it already is or another one is? - Stack Overflow

I'm using jquery and what I'm doing is binding the toggle method to a number of buttons on a

I'm using jquery and what I'm doing is binding the toggle method to a number of buttons on a webpage. It looks something like this

$('.button').toggle(function(){
  // first function
}, function(){
  // second function
});

However, there are animation in both of those functions. So a user can click the button while the first or second function is executing. And this messes up the order of the HTML elements and may make them move to the end of the page. Because essentially what these functions do is move one element to the end on the first click, and on the other click move it back where it originally was.

Of course, it is difficult to click the button once it is moving around the page. But it's possible.

I'm using jquery and what I'm doing is binding the toggle method to a number of buttons on a webpage. It looks something like this

$('.button').toggle(function(){
  // first function
}, function(){
  // second function
});

However, there are animation in both of those functions. So a user can click the button while the first or second function is executing. And this messes up the order of the HTML elements and may make them move to the end of the page. Because essentially what these functions do is move one element to the end on the first click, and on the other click move it back where it originally was.

Of course, it is difficult to click the button once it is moving around the page. But it's possible.

Share Improve this question edited Aug 31, 2009 at 20:26 Mark Biek 151k54 gold badges159 silver badges201 bronze badges asked Aug 31, 2009 at 20:12 fentfent 18.2k16 gold badges90 silver badges91 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

You could use a flag. Set a flag 'isAnimating' to true when an animation begins, and false when it ends. Any subsequent animation can only proceed if this value is false.

You could also possibly check to see if the :animated selector applies to the owner of the event. And base your decisions off of that.

You could use a bool as a semiphore.. Obviously, this is in no way secure, but javascript doesn't really support locking, so you could easily have deadlocks and / or race conditions with this approach, but it will work 99,9% of the times :)

Seems like you'll be happier implementing your own toggle. Toggle really only works for cases with 0 additional logic.

$('.button').click( 
function () {
  if( $(self).is(":animated") {
    return false;
  }
  if($(self).is(".rolledup")) {
     self.apply(roll_window_down);    
  } else {
     self.apply(roll_window_up);    
  }
});



function roll_window_up() {
  $(self).addClass( 'rolledup' );

  // first function    
}

function roll_window_down() {
  $(self).removeClass( 'rolledup' );
  // first function    
}

You need to place the two functions you pass to toggle in a context in which you can hold a flag to control function entrance:-

(function() {
  var toggling = false;
  $('.button').toggle(function(){
    if (!toggling) {
      toggling = true;           
      // first function
      toggling = false;
    } else {
      // whatever you want to happen if re-entrance attempted
    }
  }, function(){
    if (!toggling) {
      toggling = true;           
      // second function
      toggling = false;
    } else {
      // whatever you want to happen if re-entrance attempted
    }
  })
 )();

N.B. This serialises all toggles of elements that have the .button class. IOW there is only one toggling flag for all buttons. If you want each button to have its own toggling flag:-

$('.button').each(function() {
  var toggling = false;
  $(this).toggle(function(){
    if (!toggling) {
      toggling = true;           
      // first function
      toggling = false;
    } else {
      // whatever you want to happen if re-entrance attempted
    }
  }, function(){
    if (!toggling) {
      toggling = true;           
      // second function
      toggling = false;
    } else {
      // whatever you want to happen if re-entrance attempted
    }
  });
 );

You need a queue. You can build one with a semaphore variable, but jQuery already provides one, so maybe you want to use it:

$('.button').toggle(function() {
  $(document).queue("foo", function() {
    ...
  });
}, function() {
  $(document).queue("foo", function() {
    ...
  });
});

jQuery normally uses the "fx" queue to serialize animations, but you can use this "foo" queue for whatever you want.

The queue can be put on any object, so maybe you want to put it on the container that has all the .button objects in it. You cannot put it on the button (this) themselves, or you'll be back to where you're at now.

Once you've done that, all you really need to do is abort an animation. This can be done by expressly emptying the "fx" queue, or you can use $('.button').stop(); to stop all the old animations.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663246a4638965.html

相关推荐

  • 安利一个超强的linux脚本

    想像一个场景,你突然接手管理一个系统,之前对接的人没有留下任何材料,主机上也没有历史命令能够看到之前做了哪些操作。怎么办?今天介绍一个简单的脚本记录linux历史执行记录的方法,可以在现有主机上配置,然后后面主机初始化都配置上。Linux命

    1小时前
    00
  • 业内首次! 全面复现DeepSeek

    机器之心发布机器之心编辑部OpenAI 的 o1 系列和 DeepSeek-R1 的成功充分证明,大规模强化学习已成为一种极为有效的方法,能够激发大型语言模型(LLM) 的复杂推理行为并显著提升其能力。然而,这些推理模型的核心训练方法在其技

    1小时前
    00
  • 基于推理模型+RAG+Agent,作业帮内部安全体系建设实践

    文作业帮基础架构团队(吕亚霖、尚义龙) 背 景 在互联网智能化与 AI 大模型技术的双重驱动下,信息安全领域正遭遇史无前例的复杂挑战。从外部环境来看,AI 大模型的应用降低了攻击门槛。外部攻击者利用 AI 工具生成自动化攻击脚本、绕过

    57分钟前
    00
  • Adam获时间检验奖!清华揭示保辛动力学本质,提出全新RAD优化器

    新智元报道编辑:LRST【新智元导读】Adam优化器是深度学习中常用的优化算法,但其性能背后的理论解释一直不完善。近日,来自清华大学的团队提出了RAD优化器,扩展了Adam的理论基础,提升了训练稳定性。实验显示RAD在多种强化学习任务

    52分钟前
    10
  • .NET性能优化的10个关键教训:资深工程师的实战经验

    在作为高级软件工程师开发高规模.NET应用的十多年中,我亲历了众多性能挑战。有些经验来自深夜的生产事故,有些来自艰难的优化冲刺。以下是团队和我通过惨痛教训总结的十大最具影响力的性能优化实践。1. 异步等待误用的隐藏代价某订单处理微服务在高

    45分钟前
    00
  • 从0到精通,System.Text.Json进阶技巧曝光,性能提升3倍!

    一、引言在现代软件开发中,JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,被广泛应用于前后端交互、配置文件管理以及分布式系统间的数据传输。System.Text.Json 是 .NET Core

    43分钟前
    00
  • .NET 9版本支持说明

    在深入探讨.NET 9库的激动人心改进前,有必要了解微软对.NET版本的支持策略。• 奇数版本(如.NET 9):属于标准期限支持(STS),提供18个月支持周期,适合尝试前沿功能。• 偶数版本(如.NET 8或未来的.NET 10):提供

    40分钟前
    00
  • 向量数据库新标杆!qdrant v1.14.0深度解析:性能优化+AI推荐黑科技

    引言「还在为向量搜索的延迟和准确性头疼?Qdrant v1.14.0带着一堆黑科技来了!本次更新不仅优化了核心性能,还引入了服务器端打分公式和增量HNSW构建,直接让它在高负载场景下吊打Milvus、Weaviate等竞品。今天我们就来深度

    35分钟前
    00
  • 深入微服务核心:从架构设计到规模化

    《Building Microservices》这本书是吃透微服务的大部头,本文基于全书内容,系统性地阐述了微服务架构的设计原则、实施策略与挑战,从微服务的核心概念出发,延伸到架构设计、服务拆分、集成技术及规模化实践,为开发者提供了构建稳健

    31分钟前
    00
  • Java 实现 MCP Server 以及常用 MCP 服务分享

    MCP 前段时间在 AI 领域 引发了 广泛关注,特别是在 各大海内外技术社区 中,大家热烈讨论,热度非常高,本文将带领大家使用 java 语言实现一个 mcp,揭开 mcp 这神秘的面纱,本文最后也推荐给大家一些常用的 MCP 服务,开箱

    26分钟前
    00
  • JS8Core

    19分钟前
    00
  • vscode如何多行同时编辑,vscode快速选中多行快捷键

    作者:watermelo37CSDN万粉博主、华为云云享专家、阿里云专家博主、腾讯云、支付宝合作作者,全平台博客昵称watermelo37。一个假装是giser的coder,做不只专注于业务逻辑的前端工程师,Java、Do

    13分钟前
    00
  • Windows实用设置工具,你想要的系统功能它都有!

    各位系统调校爱好者们注意啦!今天给大家安利一款装机必备的Windows实用工具包,堪称系统设置的"后悔药",操作系统的"美图秀"&am

    13分钟前
    00
  • Mysql之存储过程

    1.存储过程概述 定义:MySQL 存储过程是一组预编译的 SQL 语句,它们被存储在数据库中,并可以通过调用来执行。用途:存储过程可以用于封装复杂的 SQL 操作、提高代码的重用性、增强数据操作的安全性和性能。特点:可以接受参数,也可以返

    11分钟前
    00
  • ComfyUI教程|基础篇:安装方法(Windows系统)

    前言 前言 ComfyUI作为一款功能强大的AI生图工具,它通过节点方式,使用户可以直观地看到各个模块的功能,并根据需求进行调整和连接。这种方法使工作流程更加清晰&

    10分钟前
    00
  • 从零到高手:轻松掌握蓝耘元生代 AIDC OS 中的 ComfyUI 抠图技术

    正文开始——一、引言 随着图像处理技术的不断发展,抠图作为其中的重要任务,广泛应用于电商、广告、创意设计等领域。蓝耘元生代 AIDC OS 中的 ComfyUI 工作流通过结合深度学习与先进的图像处理技术,提供了高效、精准的抠图解决方案。本

    9分钟前
    00
  • 证件照制作工具免费有哪些?这8个软件的能一键制作

    在求职、考试、签证等场景中,我们几乎每隔一段时间就需要提供新的证件照。 传统照相馆的痛点显而易见:价格高(动辄几十元)、耗时长(

    7分钟前
    00
  • 初识Redis · 持久化

    前言:本文开始,就已经算是Redis的进阶篇文章啦,因为从本文开始就要开始涉及到了Redis的一些特性和一些底层原理了。那么首当其冲的,就是Redis中的持久化了,我们提及持久化之前,我们不妨回想一下MySQL中的事务有哪些特性,一共有4个

    6分钟前
    00
  • Windows Defender:一个被低估的「系统保安」,正在改写杀毒软件战争规则

    ▍ 当代用户的认知误区:你以为的Defender vs 真实的Defender 误区① "自带杀毒肯定不如专业软件" 事实:微软拥有全球最大的威胁情报网——每月分析6.5万

    4分钟前
    00
  • SQL 多表查询:数据整合与分析的强大工具

    SQL 多表查询:数据整合与分析的强大工具在关系型数据库中,数据通常被组织在多个表中。这种表的分离有助于减少冗余并提高数据的管理效率。然而,在实际应用中,往往需要对多个表中的数据进行整合查询,来获得更完整的信息。这时候,多表查询(Join)

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信