繁体   English   中英

如何在每个会话中在一个页面中仅触发一次jquery函数?

[英]How to trigger a jquery function only once in one page, per session?

我有一个jQuery函数,它在我的index.html加载时产生动画。 我希望这个动画只发生一次。 如果你去另一个页面,然后返回index.html ,动画不应该发生。

但我还想在刷新索引页面时显示动画。

我不知道你是不是得到了......无论如何,谢谢!

Javascripts sessionStorage可以使这成为可能。

只有在创建它的窗口打开时,才能访问此sessionStorage变量。 因此,关闭窗口后,sessionStorage变量将是未定义的。

您可以使用sessionStorage.firstVisit = 1类的东西来创建它。

在if语句中简单地包装它以检查是否显示动画。

有关此检查的更多信息,请访问http://www.w3.org/TR/webstorage/#the-sessionstorage-attribute

试试这个!!

 if (!sessionStorage.isActive) {
        $('.your-div').animate(); //write your code to animate
        sessionStorage.isActive= 1;
    }

使用sessionStorage可能会有所帮助,您可以保存

animationContent = document.getElementById("yourID").innerHTML;
sessionStorage.setItem('animationContent', animationContent);
if(sessionStorage.getItem('messageContent')){
    yor animation function;
}

要么

.one确保只执行一次

$(document).once('ready', function() 
{
    Your Code here;
});

暂无
暂无

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

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