Skip to content

JavaScript setTimeout issues #2603

Answered by tabuna
jfuentesa asked this question in Q&A
May 10, 2023 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

This is roughly what your JS controller will look like

export default class extends window.Controller {

 connect() {
   this.intervalId = setInterval(() => {
     window.location.reload()
   }, 5000)
 }

 disconnect() {
   clearInterval(this.intervalId)
 }
}

But you can not update full page and make even better (More difficult for a beginner), such as update only part of the page:

export default class extends window.Controller {
    static values = {
        url: String,
        refreshInterval: Number,
    };

    connect() {
        this.load();

        if (this.hasRefreshIntervalValue) {
            this.startRefreshing();
        }
    }

    disconnect() {
        this.stopRefreshing(

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@jfuentesa
Comment options

@tabuna
Comment options

Answer selected by jfuentesa
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants