简体   繁体   English

只运行一次函数。 && 在 IE 中不起作用

[英]Run function only once. && doesn't work in IE

This function should only be able to run once, which works in Chrome and FF -- BUT In IE you can run it multiple times.这个函数应该只能运行一次,它在 Chrome 和 FF 中有效——但在 IE 中你可以多次运行它。 How can I make it able to run only once in IE ?我怎样才能让它在 IE 中只能运行一次?

function topSect() {
  var mouseY = 0;
  var topValue = 10;
  var executed = false;

  window.addEventListener("mouseout", function(e) {
      mouseY = e.clientY;
      if (mouseY < topValue && !executed) {
        //do something here
        executed = true;
      }
    },
    false);
};

Use removeEventListener to ensure that the mouseleave is ever triggered only once.使用removeEventListener确保 mouseleave 只触发一次。

 var mouseY = 0; var topValue = 10; var executed = false; function mouseoutHandler(e){ window.removeEventListener("mouseout", mouseoutHandler, false); mouseY = e.clientY; exectued = true; console.log(mouseY); } function topSect() { window.addEventListener("mouseout", mouseoutHandler, false); }; topSect();

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

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