Can one prevent Chrome causing an infinite loop when displaying alerts on the focus event, without disabling and re-enabling the event?

喜你入骨 提交于 2019-12-30 09:54:12

问题


Consider the following code. If I click in cmbMonkeys, it causes an infinite loop of alert messages in Google Chrome. My workaround for cmbPeople works fine. Does anyone know of a another way to prevent infinite loops when displaying alerts on focus or blur events, without disabling and re-enabling the event?

<html>
<head>
    <script>
        var eventHandler;

        function cmbPeople_OnFocusHandler() {
            alert("focus");
        }

        function cmbPeople_CallFocusHandler(control) {
            eventHandler = control.onfocus;
            control.onfocus = null;
            cmbPeople_OnFocusHandler();
        }

        function cmbPeople_CallBlurHandler(control) {
            control.onfocus = eventHandler;
        }

        function cmbMonkeys_FocusHandler(control) {
            alert("I like monkeys");
        }
    </script>
</head>
<body>
    monkeys <select id="cmbMonkeys" onfocus="cmbMonkeys_FocusHandler(this)"></select>
    people <select id="cmbPeople" onfocus="cmbPeople_CallFocusHandler(this)" onblur="cmbPeople_CallBlurHandler(this)"></select>
</body>
</html>

回答1:


With a simple state-handler, maybe?

var isFocus = false;
function cmbPeople_CallFocusHandler(control) {
    if(!focus){
        focus = true;
        cmbPeople_OnFocusHandler();
    }
}

function cmbPeople_CallBlurHandler(){
    isFocus = false;
}


来源:https://stackoverflow.com/questions/6151027/can-one-prevent-chrome-causing-an-infinite-loop-when-displaying-alerts-on-the-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!