Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery只运行一次函数?_Javascript_Jquery_Twitter Bootstrap_Function_Twitter Bootstrap 3 - Fatal编程技术网

Javascript 如何使用jquery只运行一次函数?

Javascript 如何使用jquery只运行一次函数?,javascript,jquery,twitter-bootstrap,function,twitter-bootstrap-3,Javascript,Jquery,Twitter Bootstrap,Function,Twitter Bootstrap 3,我有一个只允许注册用户访问的网页。 我使用引导作为CSS框架。 如果用户登录,则会出现一个警告框,其中包含如下成功消息: <div class="container"> <div class="alert alert-success" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span ari

我有一个只允许注册用户访问的网页。
我使用引导作为CSS框架。
如果用户登录,则会出现一个警告框,其中包含如下成功消息:

<div class="container">
    <div class="alert alert-success" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success!</strong> You have been signed in successfully!
    </div>
</div>
// get the cookie if its there
var popUpShown = getCookie('popUpShown');
var numberOfDaysCookieExpiresIn = 99999999999;

// check the cookie to see if the popup has been shown already
if (popUpShown != '1') {
  window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
      $(this).remove();
    });
  }, 1500);

  // set the cookie so we dont show the pop up again
  setCookie('popUpShown', '1', numberOfDaysCookieExpiresIn);
};
如何仅运行此代码一次,以便在用户意外刷新浏览器时不会再次看到此警报?

我在这里读过这篇文章 并且知道可以使用来自的
.one()
函数来完成

但是我不知道如何将它包含到我的代码中。有没有人可以帮我更正并解释,因为jquery和javascript对我来说是新的:P

你可以使用cookies。这是解释


页面加载时,您可以检查cookie,如果cookie为空,则发出警报,如果cookie不为空,则可以执行您想执行的操作。

解决方案是使用cookie,并将其日期设置为将来很远的时间

注意:此小提琴不允许您设置cookies

$(函数(){
var popup=getCookie(弹出窗口);
如果(弹出){
//显示你的警觉
$('.container')。追加(
'  '
+“×;”
+“成功!您已成功登录!”
);
}
});
setTimeout(函数(){
$(“.alert”).fadeTo(500,0).slideUp(500,function(){
$(this.remove();
//将警报cookie设置为false
document.cookie=“popup=false;expires=Thu,2090年12月18日12:00:00 UTC”;
});
}, 1500);
//获取cookies的函数
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(var i=0;i
.one()
将在每次加载页面时触发一次事件,因此对于您的示例不起作用

正如另一个答案所说,你可以使用cookies,比如:

<div class="container">
    <div class="alert alert-success" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success!</strong> You have been signed in successfully!
    </div>
</div>
// get the cookie if its there
var popUpShown = getCookie('popUpShown');
var numberOfDaysCookieExpiresIn = 99999999999;

// check the cookie to see if the popup has been shown already
if (popUpShown != '1') {
  window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
      $(this).remove();
    });
  }, 1500);

  // set the cookie so we dont show the pop up again
  setCookie('popUpShown', '1', numberOfDaysCookieExpiresIn);
};

对于刷新的浏览器,one()函数不会帮助您,您可以尝试在localStorage中设置一个变量,使用某种会话ID进行检查。如果用户刷新页面,脚本将重新加载,因此one()会不起作用。我会使用cookie或会话来执行此操作。如果会话或cookie存在,我不会运行脚本,否则它将运行.cookies或localStorage,或者只调用在登录成功函数上显示警报的函数。