Understanding Javacript SetInterval Processing

Asked 1 years ago, Updated 1 years ago, 32 views

We have reloaded an object called Tableau in SetInterval with the following code:
However, SetInterval periodically vomits the execution queue regardless of whether the reload process is complete or incomplete.If the reload process is not completed in time, the update will stop, so is there any good way?
Thank you for your cooperation.

<!DOCTYPE html>
<html lang="en">
  <head>
  <title>Tableau JavaScript API</title>
  <script type="text/javascript" src="http://servername/javascripts/api/tableau_v8.js"></script>
  </head>
  <body>
  <divid="tableauViz"></div>

<script type='text/javascript'>

varplaceholderDiv = document.getElementById("tableauViz";

  varurl="http://servername/t/311/views/Mayorscreenv5/MayorScreenv2";

  var options = {
    hideTabs —true,
    width: "100%",
    height: "1000px"
  };

  varviz = new tableauSoftware.Viz(placeholderDiv, url, options);

setInterval(function() {viz.refreshDataAsync(), 3000);

</script>

</body>
</html>

javascript html

2022-09-30 19:32

1 Answers

Describes the code to the effect that you want to wait for the reload process to complete and then reload it again after a certain period of time.

...

// Rewrite here
// setInterval(function() {viz.refreshDataAsync(), 3000);
( function loop() {
    viz.refreshDataAsync()
        .then()=>setTimeout(loop,3000))// Recursive run 3 seconds after processing is complete
        .catch(error=>console.log(error));//TODO:handling on errors
})();

...

MDN Web Docs recommends a recursive setTimeout() pattern when callback runs longer than the interval time.

If the logic may run longer than the interval time, we recommend that you recursively invoke the named function using WindowOrWorkerGlobalScope.setTimeout.

Also, according to the Tableau document, the method with the name Async returns Promise, so
You can wait for the reload process to complete by making a recursive call with the callback of the then() method.
However, the interval between reload operations in this case varies depending on how long each reload operation takes.


2022-09-30 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.