简体   繁体   English

只调用一次函数

[英]Call a function only once

I've 3 divs ( #Mask #Intro #Container ) so if you click on Mask, Intro gets hidden and Container appears. 我有3个div( #Mask #Intro #Container ),所以如果你点击Mask,Intro会被隐藏并且容器会出现。 The problem is that I just want to load this only one time, not every time I refresh the page or anytime I click on the menu or a link, etc. 问题是我只想加载一次,不是每次刷新页面或任何时候点击菜单或链接等。

How can I do this? 我怎样才能做到这一点?

This is the script I'm using for now: 这是我现在使用的脚本:

$(document).ready(function(){
    $("div#mask").click(function() {
        $("div#intro").fadeToggle('slow');
        $("div#container").fadeToggle('slow');
        $("div#mask").css("z-index", "-99");
    });
});

Thank you! 谢谢!

You can try using a simple counter. 您可以尝试使用简单的计数器。

// count how many times click event is triggered
var eventsFired = 0;
$(document).ready(function(){
    $("div#mask").click(function() {
        if (eventsFired == 0) {
            $("div#intro").fadeToggle('slow');
            $("div#container").fadeToggle('slow');
            $("div#mask").css("z-index", "-99");
            eventsFired++; // <-- now equals 1, won't fire again until reload
        }
    });
});

To persist this you will need to set a cookie . 坚持这一点,您需要设置一个cookie (eg $.cookie() if you use that plugin). (例如$.cookie()如果你使用那个插件)。

// example using $.cookie plugin
var eventsFired = ($.cookie('eventsFired') != null)
    ? $.cookie('eventsFired')
    : 0;

$(document).ready(function(){
    $("div#mask").click(function() {
        if (eventsFired == 0) {
            $("div#intro").fadeToggle('slow');
            $("div#container").fadeToggle('slow');
            $("div#mask").css("z-index", "-99");
            eventsFired++; // <-- now equals 1, won't fire again until reload
            $.cookie('eventsFired', eventsFired);
        }
    });
});

To delete the cookie later on: 要在以后删除cookie:

$.cookie('eventsFired', null);

Just point to an empty function once it has been called. 一旦被调用,只需指向一个空函数。

var myFunc = function(){
     myFunc = function(){}; // kill it
     console.log('Done once!'); // your stuff here
};

Web pages are stateless in that they don't hold states between page refreshes. 网页是无状态的 ,因为它们在页面刷新之间不保持状态。 When you reload the page it has no clue what has happened in the past. 当你重新加载页面时,它不知道过去发生了什么。

Cookies to the rescue! 饼干救援! You can use Javascript (and jQuery has some nice plugins to make it easier) to store variables on the client's browser. 您可以使用Javascript (并且jQuery有一些很好的插件使其更容易)在客户端的浏览器上存储变量。 Store a cookie when the mask is clicked, so that when the page is next loaded it never shows. 单击蒙版时存储cookie,以便在下次加载页面时它永远不会显示。

this code with will work perfect for you and it is the standard way provided by jquery to bind events that you want to execute only once 这段代码将完美适合您,它是jquery提供的标准方法,用于绑定您只想执行一次的事件

 $(document).ready(function(){
        $("div#mask").one('click', function() {
            $("div#intro").fadeToggle('slow');
            $("div#container").fadeToggle('slow');
            $("div#mask").css("z-index", "-99");
        });
    });

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

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