简体   繁体   English

流星每次刷新时如何使函数运行?

[英]How to make function run every time page refreshes in meteor?

i have the following code: 我有以下代码:

Template.home.onRendered(function() {
        var scrollElem = $("#scroll");
        scrollElem.scrollTo("max", 500);
})

I'm also using jquery-scrollto package for meteor. 我还使用流星的jquery-scrollto包。 It fires only once when page is loaded but when i refresh it never fires again. 页面加载时仅触发一次,但刷新时再也不会触发。 I know that onRendered runs only when element is changed but what should i use then? 我知道onRendered仅在元素更改时运行,但是那我应该使用什么呢? I tried to put this code in Meteor.startup() and $(window).load() but it doesn't work either. 我试图将这段代码放在Meteor.startup()和$(window).load()中,但是它也不起作用。 I fixed it by adding setTimeout() function but what if user's internet connection is slow? 我通过添加setTimeout()函数来修复它,但是如果用户的互联网连接速度慢怎么办? It wont fire. 它不会开火。 Any solutions for this? 有什么解决办法吗?

If not already doing so, try using the newer version of the package . 如果尚未这样做,请尝试使用该软件包的较新版本

It may be that your #scroll element is not loaded yet. 您的#scroll元素可能尚未加载。 You could try using the load() method: 您可以尝试使用load()方法:

Template.home.onRendered(function() {
        var scrollElem = $("#scroll");
        scrollElem.load(function () {
          scrollElem.scrollTo("max", 500);
        });
})

If it still does not work, try using the plugin directly instead of using a purely intermediary meteor package. 如果仍然无法使用,请尝试直接使用插件,而不要使用纯粹的中间流星软件包。 Uninstall the package: 卸载软件包:

meteor remove johnantoni:meteor-scrollto

In your head code, for example client/main.html : 在您的头代码中,例如client / main.html

<head>
  <script src="//cdn.jsdelivr.net/jquery.scrollto/2.1.0/jquery.scrollTo.min.js"></script>
</head>

Or, if you want to keep the lib in your app, download it and put it in public/jquery-scrollto/ , then in the head: 或者,如果您要将lib保留在您的应用程序中,请下载它并将其放在public / jquery-scrollto /中 ,然后放在头部:

<head>
  <script src="/jquery-scrollto/jquery.scrollTo.min.js"></script>
</head>

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

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