javascript JQuery - 每个会话运行一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25070877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:51:54  来源:igfitidea点击:

JQuery - Run once per session

javascriptjquery

提问by Levchik

I have a function that is triggered by "Calculate" button

我有一个由“计算”按钮触发的功能

I need this line to only run once per session (session could be 1 day or until browser is reloaded).

我需要这条线每个会话只运行一次(会话可能是 1 天或直到浏览器重新加载)。

$('.popup-with-form').magnificPopup('open');

This opens a Magnific Popup. Once this function is executed (popup opens), if "calculate" button is pressed again, I don't want popup to open again.

这将打开一个 Magnific Popup。一旦执行此功能(弹出窗口打开),如果再次按下“计算”按钮,我不想再次打开弹出窗口。

JS / JQuery code:

JS/JQuery 代码:

function StateChanged() {
       if (XmlHttp.readyState == 4 || XmlHttp.readyState == "complete") {
           $('.popup-with-form').magnificPopup('open');
          document.getElementById("CalcSum").innerHTML = XmlHttp.responseText;
          document.getElementById("CalcSumPopup").innerHTML = XmlHttp.responseText;
       }
    }

PSI know many of these questions pop up, and I tried different ways of doing thing, but since I'm "code-challanged" and do not know JQuery or JS I can't figure it out. I know there is a .one"thing" in JQuery, but don't understand how to make it work.

PS我知道很多这些问题会弹出,我尝试了不同的做事方式,但由于我是“代码挑战者”并且不知道 JQuery 或 JS,我无法弄清楚。我知道JQuery 中有一个.one“东西”,但不明白如何让它工作。

I really apreciate any help. Thanks!

我真的很感激任何帮助。谢谢!

UPDATEQuestion answered by @MarcoBonelli - Thank you!

更新@MarcoBonelli 回答的问题 - 谢谢!

Working solution:

工作解决方案:

if (!sessionStorage.alreadyClicked) {
    $('.popup-with-form').magnificPopup('open');
    sessionStorage.alreadyClicked = "true";
}

回答by Marco Bonelli

If you want to execute this line only once per browser session you can use sessionStorage. When you set a variable on sessionStorageit keeps its value until the browser closes (e.g. until you close Google Chrome).

如果您只想在每个浏览器会话中执行此行一次,您可以使用sessionStorage. 当你在sessionStorage它上面设置一个变量时,它会保持它的值直到浏览器关闭(例如,直到你关闭谷歌浏览器)。

So you can do something like:

因此,您可以执行以下操作:

if (!sessionStorage.alreadyClicked) {
    $('.popup-with-form').magnificPopup('open');
    sessionStorage.alreadyClicked = 1;
}

Be careful with sessionStoragebecause it can only store stringvalues.

小心,sessionStorage因为它只能存储string值。

If you want the line to be executed only once per page session (which means once every page refresh) then you can use any variable and set it to true to remember you already executed the line:

如果您希望该行在每个页面会话中仅执行一次(这意味着每次页面刷新一次),那么您可以使用任何变量并将其设置为 true 以记住您已经执行了该行:

if (!window.alreadyClicked) {
    $('.popup-with-form').magnificPopup('open');
    alreadyClicked = true;
}

回答by guest271314

Try

尝试

Edit, v2

编辑,v2

I read about .one but could not figure it out :( ... I actually need it to run once ONLY when CALCULATE button is pressed. – Roofing Calculator

我读过 .one 但无法弄清楚:( ...我实际上只需要在按下 CALCULATE 按钮时运行一次。 – Roofing Calculator

html

html

<!--  removed `action="javascript:GetInfo();"
              , accept-charset="UNKNOWN"
              , enctype="application/x-www-form-urlencoded" 
              , method="post"` 
      from `form` attributes -->
<form id="formcalc" style="text-align: left;">    
    <!-- changed `input` `type` to `button` -->
    <input name="calculate" type="button" value="Calculate" />
</form>

js

js

$("#formcalc > input[name='calculate']")
.one("click", function(e) {
  e.stopPropagation();
  GetInfo();
});

v1

v1

$("a.popup-with-form").one("click", function(e) {
  // do stuff, once
  // i.e.g., 
  // `XmlHttp.onreadystatechange = StateChanged;` at `ShowSum()`
  $(e.target).remove(); // remove element when `click`ed once
});

jsfiddle http://jsfiddle.net/guest271314/7K3tn/

jsfiddle http://jsfiddle.net/guest271314/7K3tn/

See http://api.jquery.com/one/

http://api.jquery.com/one/

回答by Janty

Please use below.

请在下面使用。

disableOn

禁用

null

空值

If window width is less then number in this option - lightbox will not be opened and default behavior of element will be triggered. Set to?0?to disable behavior. Option works only when you initialize Magnific Popup from DOM element.

如果窗口宽度小于此选项中的数字 - 将不会打开灯箱并触发元素的默认行为。设置为?0? 以禁用行为。选项仅在您从 DOM 元素初始化 Magnific Popup 时才有效。

Can also accept Function as a parameter, which should return?true?if lightbox can be opened andfalse?otherwise. For example:

也可以接受Function作为参数,它应该返回?真?如果灯箱可以打开,假?否则。例如:

disableOn: function() { if( $(condition) { return false; } return true; }

disableOn: function() { if( $(condition) { return false; } return true; }