简体   繁体   中英

set a timeoutfunction in wordpress jquery file

I have a piece of javascript in a template which select an option from a select dropdown and causes a .change effect when loading a page.

This works fine. However instead of having this in the template file directly i want to add the funciton in a javascript file and call it in my functions when i am on a certain page.

I have trouble setting up the timeout element of my function for a .change to happen.

Below is my working script directly in the template file

<script>

$(document).ready(function(){
$("select").val('56');

window.setTimeout(function() { jQuery('.level-1').change();}, 0.5);

});
</script>

So now my rewritten function in a file.js

This is my functions file to call the js file

add_action( 'wp_footer', 'load_js');
function load_js() {

  wp_enqueue_script( 'jquery-ui-core3', get_stylesheet_directory_uri() . '/assets/jquery331.js');


  if (is_page ('395')) { 
 wp_enqueue_script( 'scriptfile1', get_stylesheet_directory_uri() . '/assets/page395.js');
  }

}

and this is my page395.js file

jQuery(document).ready(function($){
$("select").val('56');

    window.setTimeout(function() { 
       jQuery('.level-1').change();}, 0.5);


});

I dont know how to rewrite the windpws.setTimeout function. The one above is not working. Thanks!

You have not used settimeout function properly. Try below

jQuery(document).ready(function($){
    $("select").val('56');

    setTimeout(function() { 
       jQuery('.level-1').change();
    }, 1000); //here 1000 means 1 second

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM