【发布时间】: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