Home > Net >  Reload JS function after reload DIV
Reload JS function after reload DIV

Time:12-06

the context: I have a div (#container) with js inside that return planet rise and planet set.

When I reload my div (#container) like this:

$("#container").load(location.href   " #container");

It doesn't reload my js inside it and therefore does not display planet rise and planet set.

I try to reload the js function that calcul planet rise and planet set (loadPlanetComponent()) ,like this:

function loadTonight() {

        $("#container").load(location.href   " #container");
  
        loadPlanetComponent();
    }

But when I do that, it reload my function before or at the same time it reload my div so variables (planet rise and planet set) are not displayed.

Also I try 2 things:

  • Put a delay to reload loadPlanetComponent(), but sometimes, when the div takes time to load, it doesn't work

  • Wait the reload of the div but it does'nt work.

CodePudding user response:

jQuery's load() function accepts a callback function to execute after the status is set to complete:

If a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed.

So to solve the issue, you simply need to provide a callback function to your load() function:

$("#container").load(location.href   " #container", function(){ 
    loadPlanetComponent();
});
  • Related