function runs on document.ready instead of when told to

185 views Asked by At

Quite new to jQuery. I have a function which does what I want... almost. It begins like this:

var myFunction = function () {
    "use strict";

    jQuery(function ($) {

    //various things

    });

};

Things is, I want to call it when this other function happens:

jQuery(function ($) {
    "use strict";

    $(document).ready(function () {
        anotherFunction;
        myFunction;
    });    

});

Instead it seems that the first function runs by itself when the document is ready, and anotherFunction is called only in a second moment.

Does it depends on the way the first function begins? Is that a good start? I looked around for examples and solutions, but there are so many different ways to start a function in jQuery I couldn't figure out which one is the one I need.

Can you help me frame these two functions, so that the first one won't run until the second one calls it?

I know I need the jQuery(function ($) bit otherwise browser tells me $ is not a function...

1

There are 1 answers

6
Ducain On BEST ANSWER
function one() {
  //Do stuff
};

function two() {
  one(); //Calls first function

  //Do other stuff
}

$(document).ready(function () {
  two(); //Executes second function on doc ready
}); 

This may get you where you want to go. That said, if you don't want anything to execute on doc ready, you can just take that part out.