[英]How to run a function only once with jquery?
I have a webpage where only registered users can be. 我有一个网页,只有注册用户可以。
I use Bootstrap as CSS Framework. 我使用Bootstrap作为CSS框架。
If the user logged in there will be a alert box with a successmessage like this: 如果用户登录,将出现一个带有如下成功消息的警报框:
<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">×</span></button>
<strong>Success!</strong> You have been signed in successfully!
</div>
</div>
Now i want to close this after a few seconds. 现在,我想在几秒钟后关闭它。 I use this jquery script to do that:
我使用这个jQuery脚本来做到这一点:
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 1500);
How can I run this code only once, so that if the user accidently refreshed the browser does not see this alert again ? 我如何只运行一次此代码,以便如果用户不小心刷新浏览器就不会再次看到此警报?
I've read this post here https://stackoverflow.com/a/23859937/5930557 and know that it can be done with the .one()
function from http://api.jquery.com/one/ 我已经在这里https://stackoverflow.com/a/23859937/5930557阅读了这篇文章,并且知道可以使用http://api.jquery.com/one/中的
.one()
函数来完成此操作
But i dont know how to include it into my code. 但是我不知道如何将其包含到我的代码中。 Is someone there who can correct it for me and explain, cause jquery and javascript are new to me :P
那里有人可以为我更正并解释,因为jquery和javascript对我来说是新的:P
A solution would be to use a cookie and set its date to very far in the future. 一种解决方案是使用cookie并将其日期设置为将来的很远。
Note: This fiddle won't allow you to set cookies. 注意:此小提琴不允许您设置cookie。
$(function(){ var popup = getCookie(popup); if(popup){ //Dislay your alert $('.container').append( ' <div class="alert alert-success" role="alert"> ' + '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>' + '<strong>Success!</strong> You have been signed in successfully! </div>' ); } }); window.setTimeout(function() { $(".alert").fadeTo(500, 0).slideUp(500, function(){ $(this).remove(); //Set the alert cookie to false document.cookie = "popup=false; expires=Thu, 18 Dec 2090 12:00:00 UTC"; }); }, 1500); //Function to get cookies function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i <ca.length; i++) { var c = ca[i]; while (c.charAt(0)==' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length,c.length); } } return ""; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> </div>
.one()
will trigger an event once, everytime the page is loaded so wouldn't work for your example. .one()
将在每次页面加载时触发一次事件,因此不适用于您的示例。
As another answer has said, you could use cookies, something like this: 正如另一个答案所说,您可以使用Cookie,如下所示:
// 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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.