Javascript 我的按钮设置超时触发器未加载

Javascript 我的按钮设置超时触发器未加载,javascript,Javascript,我有一个按钮,可以在页面加载后自动单击。我使用了setTimeout,但它似乎不起作用。怎么了?它在stackoverflow中的其他类似帖子中也起到了作用,这也是我的想法来源,但由于一些未知的原因,在我的代码中不起作用 $(document).ready(function() { jQuery(function($){ // The variable jcrop_api will hold a re

我有一个按钮,可以在页面加载后自动单击。我使用了setTimeout,但它似乎不起作用。怎么了?它在stackoverflow中的其他类似帖子中也起到了作用,这也是我的想法来源,但由于一些未知的原因,在我的代码中不起作用

    $(document).ready(function() {
                    jQuery(function($){
                        // The variable jcrop_api will hold a reference to the
                        // Jcrop API once Jcrop is instantiated.
                        var jcrop_api;

                        initJcrop();

                        // The function is pretty simple
                        function initJcrop()//{{{
                        {

                          // Invoke Jcrop in typical fashion
                          $('#target').Jcrop({
                            onRelease: releaseCheck,

                          },function(){

                            jcrop_api = this;
                            // Setup and dipslay the interface for "enabled"
                            $('#can_move').attr('checked','checked');

                          });

                        };
                        function releaseCheck()
                        {
                          jcrop_api.setOptions({ allowSelect: true });
                          $('#can_click').attr('checked',false);
                        };



                        $('#setSelect').click(function(e) {
                          // Sets a random selection
                          jcrop_api.setSelect([0,0,400,118]);
                        });

                      });
         setTimeout(function() {
            $('#setSelect').trigger('click');
        },10);
    });

<button id="setSelect">setSelect</button>
$(文档).ready(函数(){
jQuery(函数($){
//变量jcrop_api将保存对
//Jcrop实例化后的Jcrop API。
var jcrop_api;
initJcrop();
//这个函数非常简单
函数initJcrop()//{{{
{
//以典型的方式调用Jcrop
$('#target').Jcrop({
onRelease:releaseCheck,
},函数(){
jcrop_api=这个;
//设置并删除“已启用”的界面
$('can#u move').attr('checked','checked');
});
};
函数释放检查()
{
jcrop_api.setOptions({allowSelect:true});
$('can#u click').attr('checked',false);
};
$(“#设置选择”)。单击(函数(e){
//设置随机选择
jcrop_api.setSelect([0,0400118]);
});
});
setTimeout(函数(){
$('#setSelect')。触发器('click');
},10);
});
集选

事情的异步顺序可能存在问题:

    $(document).ready(function() {
                    jQuery(function($){
                        // The variable jcrop_api will hold a reference to the
                        // Jcrop API once Jcrop is instantiated.
                        var jcrop_api;

                        initJcrop();

                        // The function is pretty simple
                        function initJcrop()//{{{
                        {

                          // Invoke Jcrop in typical fashion
                          $('#target').Jcrop({
                            onRelease: releaseCheck,

                          },function(){

                            jcrop_api = this;
                            // Setup and dipslay the interface for "enabled"
                            $('#can_move').attr('checked','checked');

                          });

                        };
                        function releaseCheck()
                        {
                          jcrop_api.setOptions({ allowSelect: true });
                          $('#can_click').attr('checked',false);
                        };



                        $('#setSelect').click(function(e) {
                          // Sets a random selection
                          jcrop_api.setSelect([0,0,400,118]);
                        });

                      });
         setTimeout(function() {
            $('#setSelect').trigger('click');
        },10);
    });

<button id="setSelect">setSelect</button>
例如,
setTimeout
在设置
click
处理程序之前弹出,或者可能在设置
click
处理程序之后,但在设置
jcrop\u api
之前弹出。请注意,仅仅因为一行在另一行之下并不意味着它以后执行,因为您无法控制初始化
jcrop的回调的时间_调用api

    $(document).ready(function() {
                    jQuery(function($){
                        // The variable jcrop_api will hold a reference to the
                        // Jcrop API once Jcrop is instantiated.
                        var jcrop_api;

                        initJcrop();

                        // The function is pretty simple
                        function initJcrop()//{{{
                        {

                          // Invoke Jcrop in typical fashion
                          $('#target').Jcrop({
                            onRelease: releaseCheck,

                          },function(){

                            jcrop_api = this;
                            // Setup and dipslay the interface for "enabled"
                            $('#can_move').attr('checked','checked');

                          });

                        };
                        function releaseCheck()
                        {
                          jcrop_api.setOptions({ allowSelect: true });
                          $('#can_click').attr('checked',false);
                        };



                        $('#setSelect').click(function(e) {
                          // Sets a random selection
                          jcrop_api.setSelect([0,0,400,118]);
                        });

                      });
         setTimeout(function() {
            $('#setSelect').trigger('click');
        },10);
    });

<button id="setSelect">setSelect</button>
添加
console.log
行可能有助于调试此问题,并查看代码是否按您计划的顺序执行

    $(document).ready(function() {
                    jQuery(function($){
                        // The variable jcrop_api will hold a reference to the
                        // Jcrop API once Jcrop is instantiated.
                        var jcrop_api;

                        initJcrop();

                        // The function is pretty simple
                        function initJcrop()//{{{
                        {

                          // Invoke Jcrop in typical fashion
                          $('#target').Jcrop({
                            onRelease: releaseCheck,

                          },function(){

                            jcrop_api = this;
                            // Setup and dipslay the interface for "enabled"
                            $('#can_move').attr('checked','checked');

                          });

                        };
                        function releaseCheck()
                        {
                          jcrop_api.setOptions({ allowSelect: true });
                          $('#can_click').attr('checked',false);
                        };



                        $('#setSelect').click(function(e) {
                          // Sets a random selection
                          jcrop_api.setSelect([0,0,400,118]);
                        });

                      });
         setTimeout(function() {
            $('#setSelect').trigger('click');
        },10);
    });

<button id="setSelect">setSelect</button>

要解决问题的顺序,您可以仅在初始化jcrop_api后设置单击处理程序,并仅在设置单击处理程序后设置超时(可能根本不需要超时)。

前两行是同义词。请仅使用其中一行。
    $(document).ready(function() {
                    jQuery(function($){
                        // The variable jcrop_api will hold a reference to the
                        // Jcrop API once Jcrop is instantiated.
                        var jcrop_api;

                        initJcrop();

                        // The function is pretty simple
                        function initJcrop()//{{{
                        {

                          // Invoke Jcrop in typical fashion
                          $('#target').Jcrop({
                            onRelease: releaseCheck,

                          },function(){

                            jcrop_api = this;
                            // Setup and dipslay the interface for "enabled"
                            $('#can_move').attr('checked','checked');

                          });

                        };
                        function releaseCheck()
                        {
                          jcrop_api.setOptions({ allowSelect: true });
                          $('#can_click').attr('checked',false);
                        };



                        $('#setSelect').click(function(e) {
                          // Sets a random selection
                          jcrop_api.setSelect([0,0,400,118]);
                        });

                      });
         setTimeout(function() {
            $('#setSelect').trigger('click');
        },10);
    });

<button id="setSelect">setSelect</button>