我没有加载按钮的settimeout触发器

时间:2013-02-05 14:40:11

标签: javascript

我有一个按钮,我可以在加载页面后自动点击该按钮。我使用了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>

1 个答案:

答案 0 :(得分:1)

异步订单可能存在问题:

例如。在设置setTimeout处理程序之前弹出click,或者在click处理程序设置之后但在设置jcrop_api之前弹出jcrop_api。请注意,仅仅因为一行低于另一行并不意味着它稍后执行 - 因为您无法控制何时调用初始化console.log的回调。

添加{{1}}行可能对调试此问题很有用,并查看代码是否按照您计划的顺序执行。

要修复事物的顺序,只有在初始化jcrop_api之后才能设置单击处理程序,并且只在设置了单击处理程序后设置超时(可能根本不需要超时)。 / p>