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

标签 javascript jquery ajax-upload

我在新窗口对象创建和链接单击之间遇到竞争条件。新窗口是一个名为 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 - 新窗口对象和链接单击之间的竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31034218/

相关文章:

JavaScript 变量赋值/返回

将 bool 值赋值给 html 对象的 JavaScript 代码语法

javascript - 根据id从文本输入中获取 boolean 值

ajax - 使用 Jeditable + ajaxUpload 的问题

javascript - JavaScript 中的 "$$("a")"是什么意思?

javascript - 如何在 OpenLayers 的矢量图层上以编程方式选择特征?

java - jquery datatables-一个数据表中的最大列数

javascript - Jquery 更改未检测到选择更改 rails

使用 AjaxUpload 的 Python 图片上传