Trigger polling using htmx.trigger

I'm trying to trigger a polling behavior on a div using JavaScript.

This is the div.

<div id="myId" hx-get="https://xxxx" hx-swap="innerHTML"></div>

This is the trigger function.

 htmx.trigger(
   htmx.find(`#myId`),
   "every 300ms"
 );

But it's not working. Is there a way to obtain this behavior?

1 answer

  • answered 2022-05-06 18:40 pthomson

    you may need to have your javascript set up the polling interval. Then you can trigger a custom event and have your div listen for it. Also innerHTML is the default hx-swap target, so could be omitted.

    HTMX:

    <div id="myId" hx-get="https://xxxx" hx-trigger="myEvent"></div>
    

    JS:

    setInterval(function () {  htmx.trigger("myId","myEvent"); }, 300);
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum