简体   繁体   English

如何防止点击和模糊事件之间的竞争?

[英]How to prevent racing between click and blur event ?

my Angular code look like this >我的 Angular 代码看起来像这样 >

<input type='text' (blur)='saveData()'>
<button (click)='navigateToPage2()'>

Jumping from textbox being focused to the button click , there is racing condition between two events.获得焦点的文本框跳转到按钮 click ,两个事件之间存在竞争条件。

Problem is button click gets ignored because the blur event fires first and brings the spinner on the screen while its making server call to save data.问题是按钮点击被忽略,因为模糊事件首先触发并将微调器带到屏幕上,同时它进行服务器调用以保存数据。

How do I solve this exact problem?我该如何解决这个确切的问题?

Is there a hack to make click event fire before blur event ?在模糊事件之前是否有一个技巧可以让点击事件触发?

Instead of such hack to fire click before blur event try following solution :而不是这种在模糊事件之前触发点击的黑客尝试以下解决方案:

To understand this better you need to know this Order in which specific event fires:为了更好地理解这一点,您需要知道特定事件触发的顺序:

  1. mouse down鼠标按下
  2. blur模糊
  3. mouse up鼠标向上
  4. click点击

You can use this order to work in your favor in this case.在这种情况下,您可以使用此命令对您有利。

Try using Mousedown event instead and that will fire before blur event.尝试改用 Mousedown 事件,它会在模糊事件之前触发。

Also, try running this code snippet.此外,尝试运行此代码片段。 It will help you understand the above order它将帮助您理解上述顺序

 Type first in text box below and then click button <br/> <input type='text' onblur="console.log('blur');" /> <button onclick="console.log('click')" onmouseup="console.log('mouseup')" onmousedown="console.log('mousedown')" > Type first and then this click button</button>

根据此示例,您还可以在 mouseDown 事件上使用e.preventDefaulthttps : e.preventDefault (他使用了 jquery,但它应该与其他库相同)

如果您正在收听(blur)="onBlur()"包的内容onBlur()setTimout与持续时间100毫秒以上。

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

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