新窗口对象和链接单击之间的竞争条件

A race condition between new window object and link click

本文关键字:之间 单击 竞争 条件 链接 窗口 对象 新窗口      更新时间:2023-09-26

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

该页面需要许多链接,这些链接会带来具有唯一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");

要正确执行吗?

这个问题无关紧要。没有出现竞赛情况。问题是,无论是什么原因,模拟点击都不会打开窗口。这个问题已经结束。