新 window 对象和 link 单击之间的竞争条件

A race condition between new window object and link click

我在新 window 对象创建和 link 单击之间遇到竞争条件。新的 window 是一个名为 AjaxUpload 的插件,其输入需要具有唯一 ID 的 link 元素。请注意,AjaxUpload 带来了新的 window 文件选择。

该页面需要许多 link 来使文件选择 windows 具有唯一 ID。因此为了简化场景,计划是为点击的link附加一个新ID,创建window对象,模拟鼠标点击调出window,销毁ID,对其他 link.

做同样的事情

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

代码如下:

$(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");

能正常执行吗?

这个问题无关紧要。没有发生竞争条件。问题是,无论什么原因,模拟点击都不会显示 window。此问题已关闭。