挂起事件监听器 [英] Suspending event listeners

查看:87
本文介绍了挂起事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有onclick事件监听器的按钮(在MooTools中).它工作正常.但是,我希望能够使按钮在特定时间不可用,并在必要时使其再次可用.

I've got a button with an onclick event listener attached (in MooTools). It works fine. However, I want to be able to make the button unavailable at a certain time, and make it available again when necessary.

简单地说:我想挂起"事件监听器.能做到吗? MooTools文档仅涉及事件的添加,删除,克隆和传播.暂时不要忽略"它们.

Simply put: I want to 'suspend' the event listener. Can this be done? The MooTools docs only go into adding, removing, cloning and propagation of events. Not 'ignoring' them for a while.

我知道解决方案可以在其他地方进行

I understand the solution for this can lie elsewhere:

  1. 使用一些CSS隐藏按钮,例如$('button').setStyle('visibility', 'hidden'),让它稍后再显示
  2. 一些叠加层,再次隐藏原始元素,并显示替换项(例如,灰色按钮)
  3. 克隆元素,从原始文件中删除所有事件(使按钮不可用"),然后将原始文件替换为克隆文件(从而使其再次可用)
  4. 让事件监听器附带的功能检查按钮是否可用
  1. Hide the button with some css, e.g. $('button').setStyle('visibility', 'hidden') and let it show up again later
  2. Some overlay, again hiding the original element, and showing a substitute (grayed out button for instance)
  3. Cloning the element, removing all the events from the original (making the button 'unavailable'), then replace the original with the clone (thus making it available again)
  4. Let the function attached to the event listener check if the button is available or not

但是,这有点不重要了,似乎比需要的还要复杂.我的问题是,是否可以以及如何控制事件监听器,基本上是:我可以将其挂起吗?

However, this is a bit beside the point and seems more elaborate then necessary. My question is if and how I can control the event listener, basically: can I suspend it?

我猜答案是否".这意味着要执行上述四项操作之一,或者执行我接受的答案中提到的四项操作:禁用按钮HTML元素,从而暂停对事件侦听器的响应.

I guess the answer is 'no'. This means doing one of the four things mentioned above, or as mentioned in the answer I accepted: disable the button HTML-element, thus suspending the response to event listeners.

推荐答案

我将看到两种方法:

  1. 禁用该元素.当元素处于disabled状态时,将在其上禁用事件侦听器.
  2. 删除事件监听器.这意味着一旦想再次启用它,就必须重新添加它.这意味着您可以添加代码以将事件添加到函数中,以便事件可重用.
  1. Disable the element. When the element has the disabled state, event listeners are disabled on it.
  2. Remove the event listener. That means you have to re-add it once you want to enable it again. Which means you may add your code to add the event in a function so that it is reusable.

这篇关于挂起事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆