javascript - turn off event listener after triggered once -


I have a script that triggers popup box that provides free downloads, it starts when someone The user's mouse leaves the page (obviously not for mobile - I already have a work solution for that). In my script I set a cookie to deactivate the script for 30 minutes, and that part works; Popovar does not appear again if I refresh the page.

The problem is that if a popover closes the box, and the page has not refreshed (or navigated to another page on the site), the popover gets out of the mouse page every time .

I am using a function that sets an event, but I can not do it only once in the fire. (The WordPress site I'm working with is using jquery ver = 1.10.2).

My Javascript:

  if (Document.cookie.indexOf ('somecookiename')! = -1) {function addEvent (obj, evt, fn) {if (obj.addEventListener) {obj.addEventListener (evt, fn, false); } And if (obj.attachEvent) {obj.attachEvent ("on" + + evt, fn); }}}; JQuery (document) .ready (function ($) {if (document.cookie.indexOf ('somecookiename') == -1) {// thirty minutes = 1000 * 60 * 30; var thirty minutes = 1000 * 60; Var expires = new date ((new date ()). ValueOf () + thirty minutes); document.cookie = "somecookiename; expires =" + expires.toUTCString (); addEvent (document) , "Mouseout", function (e) {e = E? E: from window.event; var to = e.relatedTarget || e.toElement; if (.nodeName == "html" from!) {Document.getElementById (" Preview-download "). Style.display =" block "; document.getElementById (" back-shader ") style.display =" block ";}}})}}); Function ShowHead (ID) {if (document.getElementById) {obj = document.getElementById (ID); If (obj.style.display == "none") {obj.style.display = ""; } And {obj.style.display = "none"; }}}  

HTML:

  & lt; Div id = "back-shader" class = "popover2" & gt; & Lt; Div id = "preview-download" class = "popover2" & gt; & Lt; H4 & gt; Before you go: & lt; / H4 & gt; & Lt; H5 & gt; Download a free preview .... & lt; / H5> & Lt; A href = "http://website.com/document.pdf" id = "preview-pdf" & gt; Download Now & lt; / A & gt; & Lt; A href = "#" id = "close" onclick = "showhead ('back-shader'); return (wrong);" & Gt; & Lt; / A & gt; & Lt; / Div & gt; & Lt; / Div & gt;  

CSS:

  #back-shader, # preview-download {display: none; }  

This may be a bit easier, but I'm not a JavaScript expert, your help, as usual, is appreciated.

Using the easiest method, the event is removing the listener:

 < Code> document.addEventListener ("mouseout", function handler (e) {e.currentTarget.removeEventListener (e.type, handler); / * whatever);  

< Pre class = "snippet-code-js lang-js prettyprint-override"> document.addEventListener ("click", function handler (e) {e.currentTarget.removeEventListener (e.type, handler); warning Only once ');});
  click anywhere  
< / P>

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -