跳到主要内容
版本:v2.0.0

Run query at specified intervals

In this how-to guide, we will learn how to make a query trigger at the specific intervals.

  • Let's go to the ToolJet dashboard and create a new application

  • Once the app builder opens up, drag a table component to canvas

  • Now, create a new REST API query from the query panel at the bottom of the app builder. We will be using the data from the mock REST API and then load the data on the table. Let's create a REST API, choose GET method from the dropdown, enter the endpoint (https://jsonplaceholder.typicode.com/posts), name the query post and then save and run it

    REST API query
  • Go to the Table properties and add connect the query data to table by adding value to table data property which is {{queries.post.data}}

    REST API query
  • Now, we will create a RunJS query that will first set a variable called interval which will include the value returned by the setInterval() method that calls a function countdown at specified intervals. The countdown function has the code to trigger the post query that we created in the previous step.

    actions.setVariable('interval',setInterval(countdown, 5000));
    function countdown(){
    queries.post.run()
    }
  • Go to the Advanced tab of the query, enable Run query on page load? this will trigger this RunJS query when the app is loaded. Name the query as set and Save it. Note that you will have to save the query and not Save and Run because doing it will trigger the query and you won't be able to stop the query unless you reload the page or go back to dashboard.

    REST API query
  • To prevent the query from triggering indefinitely, we will create another RunJS query that will make use of clearInterval() method. In this method we will get the value from the variable that we created in set query. Save this query as clear.

    clearInterval(variables.interval)
  • Finally, let's add a button on to the canvas and add the event handler to the button to run the clear query.

  • Now, whenever the app will be loaded the set query will be triggered and will keep triggering the post query at the specified intervals. Whenever the user wants to stop the query they can click on the button to trigger the clear query which will clear the interval.