Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got javascript (a) and jQuery script (b). I need (b) to run after (a) has finished running. I need them to run sequentially. How do I do that?

In wp_enqueue_script, I've made (b) dependent on (a), but it appears that (b) runs before (a) has finished processing.

I'm trying to use the_maps_bounds, a variable I've put in the global scope. It gets set in (a) and used in (b). The problem is that (b) is trying to use the variable before it has been set.

Any suggestions?

Thank you.

share|improve this question
1  
You need to post the code for (a) so we can see how/when/where is the variable set –  onetrickpony Sep 29 '11 at 18:18
    
FYI: jQuery IS javascript. One and the same. –  Chris Sep 29 '11 at 18:22
add comment

1 Answer

up vote 0 down vote accepted

To accomplish this, wrap wp_enqueue_script in a function and run it through the action hook wp_enqueue_scripts while using the 'priority' parameter available in add_action to set the load order.

function these_go_first() {
    wp_enqueue_script('first_script', '[path to file]/first.js', array('jquery'), '1.0' );
}
function these_go_first() {
    wp_enqueue_script('after_script', '[path to file]/after.js', array('jquery', 'first_script'), '1.0' );
}
add_action('wp_enqueue_scripts', 'these_go_first', 1);
add_action('wp_enqueue_scripts', 'these_go_after', 2);

wp_enqueue_script can also define script dependencies as well. This is why I added 'first_script' which I had already defined in the function just before, to the dependencies array in the second function.

share|improve this answer
    
I doubt that this is the issue here. He is probably setting that variable after an ajax event or something... –  onetrickpony Sep 29 '11 at 19:14
    
hmmm... well running the scripts in the correct order using this could help. –  Drew Gourley Sep 30 '11 at 0:17
    
Hi @DrewGourley Thank you for the help. It was a timing problem. My function was firing before the map fully loaded. I added your code, and the timing was still off. So in addition, I added an eventListener. It works now. Thank you. –  Laxmidi Sep 30 '11 at 15:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.