javascriptjqueryfunctioncallbackjquery-callback

jQuery - Call one function by hover, then call another function


So I have a submenu that loads content when a main nav button is hovered over. And while that content is loading I want to display a loading graphic.

The problem is I can't get both functions working together.

1st function - load submenu content (page.php)

2nd function - when page.php is loaded, hide loading graphic

<script>
function functionOne() {
jQuery.get('page.php', function(data) { jQuery('.class').html(data); })
}

function functionTwo() {
  jQuery("#loading").hide();    
};

jQuery(".dropdownhover").one('mouseenter', functionOne.done( functionTwo ););
</script>

<a class="dropdownhover">Button</a>
<div id="loading"></div>

With the above code, neither function works. If I call just the first function like this: jQuery(".dropdownhover").one('mouseenter', functionOne ); that works fine. But I of course need to call both, one after the other. Am I just writing the last line of the script wrong?


Solution

  • As per your requirement, the syntax that you have used is completely wrong, there is no such function done available in Function's prototype.

    You can achieve that by just writing,

    function functionOne() {
     jQuery.get('page.php', function(data) { 
       jQuery('.class').html(data); 
       functionTwo(); 
     });
    }
    
    function functionTwo() {
      jQuery("#loading").hide();    
    };
    
    jQuery(".dropdownhover").one('mouseenter', functionOne);