改变窗口后的Safari模糊事件循环 [英] Safari blur event loop after changing window
问题描述
我想通过警告框对模糊事件做出反应。我们的想法是验证内容并在值无效时给出反馈。我没有使用警报框进行调试,我的客户坚持要求给予用户反馈。
我在这里有一个相似之处:(但这个问题并不多余!) Focusout事件循环
I want to react to a blur event with a alert box. The idea is to validate the content and give feedback if the the value is not valid. I am not using the alert box for debugging, my client insists on that for giving the user feedback. I had a similar here: (But this question is not redundant!) Focusout event loop
所以我现在的解决方案就是这样
HTML:
Type some stuff here:
<br>
<input type="text" id="test" />
JavaScript:
JavaScript:
var doFocus = function () {
$("#test").focus();
console.log("do focus");
};
$("#test").blur(function () {
console.log("Blur event got triggered.");
alert("Blur event got triggered.");
window.setTimeout(function () {
doFocus();
}, 1);
});
到目前为止这是有效的,但
问题是:如果您打开网站,请单击输入字段,然后更改窗口。例如,打开文本编辑器,然后更改回来。 Safari陷入事件循环(一次又一次地调用回调)......
This works so far, but the Problem is: if you open the site, click into the input field and then change the window. For example open a text editor, and then change back. The Safari gets stuck in a event loop (the callback is called again and again)...
推荐答案
只需删除此行:
alert("Blur event got triggered.");
在任何模糊事件回调函数中都不应使用Alert()。
Alert() should never been used in any blur event callback function.
更新
var doFocus = function () {
$("#test").focus();
console.log("do focus");
};
var bluring = function(sender){
$(sender).off('blur');
console.log("Blur event got triggered.");
alert("Blur event got triggered.");
window.setTimeout(function () {
doFocus();
$(sender).on('blur',function(){bluring(this)});
}, 1);
};
$("#test").blur(function(){bluring(this)});
这篇关于改变窗口后的Safari模糊事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!