简体   繁体   English

在wordpress jquery文件中设置超时功能

[英]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. 我的模板中有一段JavaScript,可从选择下拉列表中选择一个选项,并在加载页面时导致.change效果。

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. 但是,我不想在模板文件中直接包含此功能,而是想在JavaScript文件中添加功能,并在我在某个页面上时在函数中调用它。

I have trouble setting up the timeout element of my function for a .change to happen. 我无法设置函数的超时元素以使.change发生。

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 所以现在我在file.js中重写了函数

This is my functions file to call the js file 这是我调用js文件的函数文件

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 这是我的page395.js文件

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. 我不知道如何重写windpws.setTimeout函数。 The one above is not working. 上面的一个不起作用。 Thanks! 谢谢!

You have not used settimeout function properly. 您没有正确使用settimeout函数。 Try below 试试下面

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

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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