【问题标题】:A race condition between new window object and link click新窗口对象和链接点击之间的竞争条件
【发布时间】:2015-06-24 18:28:17
【问题描述】:

我在新窗口对象创建和链接点击之间遇到了竞争条件。新窗口是一个名为 AjaxUpload 的插件,其输入需要具有唯一 ID 的链接元素。请注意,AjaxUpload 会打开一个新的文件选择窗口。

该页面需要许多链接,这些链接会带来具有唯一 ID 的文件选择窗口。因此为了简化场景,计划是给点击的链接附加一个新的ID,创建窗口对象,模拟鼠标点击弹出窗口,销毁ID,对其他链接做同样的事情。

但是,在窗口对象完成加载之前执行模拟点击时会出现问题,导致代码仅在链接被点击两次时才有效。

代码如下:

$(document).ready(function() {
            // The link is an anchor element with icon camera class
            // that will be attached with a new ID called wall-image-upload
            // which will destroyed after the window is brought up
            $( "a.icon.camera" ).click(function(e) {
                // Exit the function when wall-image-upload
                // id is created to avoid infinite loop
                if ($('#wall-image-upload').length!==0) {
                    return;
                }
                // Create the ID
                e.target.setAttribute("id", "wall-image-upload");

                // Create AjaxUpload object to handle the
                // image attachment where it looks up link 
                // with wall-image-upload ID
                var uploader = new window.AjaxUpload(
                   'wall-image-upload',
                   { action: 'wall_upload/{{$nickname}}',
                       name: 'userfile',
                       onSubmit: function(file,ext) { $('#profile-rotator').show(); },
                       onComplete: function(file,response) {
                           addeditortext(response);
                           $('#profile-rotator').hide();
                       }                 
                   }
                );
             // Simulate click on the element, this doesn't effect on
             // anything unfortunately
             $('#wall-image-upload').trigger("click");

             // Destroy the id
             $('#wall-image-upload').prop("id",null);

            }); 
});

我应该在哪里以及如何放置

$('#wall-image-upload').trigger("click");

要正确执行?

【问题讨论】:

    标签: javascript jquery ajax-upload


    【解决方案1】:

    这个问题无关紧要。没有发生竞争条件。问题是,无论原因是什么,模拟点击都不会打开窗口。此问题已结束。

    【讨论】: