Search code examples
htmx

How to wait for a HTMX request to finish, and then trigger a new HTMX request?


How to fix this? I tried also htmx:xhr:loadend and htmx:afterrequest , in both it was the same problem, only with timeout was working

document.addEventListener('htmx:afterSwap', function(evt) {

    //.... wait for a POST with path /add-song ....
    
    //DOESNT WORK, i cant trigger new HTMX event, immediatly, its ignored
    lastSong.dispatchEvent(new Event("play-song-event"));

    //WORKS AFTER 1sec
    setTimeout(function ()
    {
      lastSong.dispatchEvent(new Event("play-song-event"));
    },1000);
    
});

Solution

  • Found it, i needed 'htmx:afterSettle' instead of 'htmx:afterSwap'