简体   繁体   中英

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. How can I make it able to run only once in 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.

 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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