JQuery in Rails is failing after linking from another page, works on page load

后端 未结 1 1429
离开以前
离开以前 2020-12-11 11:38

I have a quote rotator I use on my homepage. When I load the page from the browser directly (type the address in the browser and hit enter) it works fine.. However, if I cli

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 12:09

    I'd guess that this is Turbolinks related. From the fine manual:

    Events

    With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

    AFAIK, Rails4 enables turbolinks by default (and you have it in your application.js) so $(function() { ... }) won't always fire when changing pages. You could try binding to turbolinks:load instead:

    $(document).on('turbolinks:load', function() {
        setInterval(rotateQuotes, 5000);
    });
    

    You might want to bind to turbolinks:before-visit to clean things up as well.

    Alternatively, you could disable Turbolinks if you don't care about it.

    0 讨论(0)
提交回复
热议问题