Triggering JS Query first time my app loads

I have a "JS Code" query that uses setTimeout to regularly poll one of our REST APIs so I can display semi-live state that our users need to see in our retool app. Right now I have a button that I press to start this polling mechanism, but I want to start it as soon as the retool app is loaded automatically.

The JS Code resource says my only option is to "Run query only when manually triggered", is there a place in retool where I can .trigger my query at startup?

My timeout polling code looks roughly like this (where getServerState is a RESTQuery query)

const POLL_TIMEOUT = 20 * 1000;

const doUpdate = () => {
  getServerState.trigger({ onSuccess: (data) => { 
    const serverTime = data.serverTime; 
    console.log("server time = " + serverTime); 

    // queue up another call
    setTimeout(doUpdate, POLL_TIMEOUT);
}})};

doUpdate();

1 Like

You could use an empty query that runs when the page loads that simply returns nothing and then upon success run the js query

1 Like

Hi @Baback! You can navigate to the advance tab of the query you want to load and tick the option Run this query on page load. Setting your expectations that whenever you refresh the app, this query will be triggered.

LMK if it is what you are looking for.

EDIT: added screenshot for reference and grammar (LOL)

3 Likes

Yep. Totally missed that. Good call

I do not see that option.

Whoops - Your query has to be like this: