Search code examples
htmx

htmx link autofresh until output does not change


If I press this hx-get link I get the updated time without refreshing the entire page.

<td style="White-space: nowrap;">
  <div hx-get="/jobtime/2416" hx-swap="outerHTML" class="">157.0</div>
</td>

Is it possible to auto-load this url like every 1minute, with a check that if it does not increase we stop?

<td style="White-space: nowrap;">
  <div hx-get="/jobtime/2416" 
      hx-swap="outerHTML"
      class=""
      hx-trigger="every 60s [someConditional]"
  >157.0</div>
</td>

Solution

  • Yes. If you no longer want to continue auto-load, you can return specifc response code to make it stop.

    Please refer to this section: https://htmx.org/docs/#polling

    If you want to stop polling from a server response you can respond with the HTTP response code 286 and the element will cancel the polling.