我有一个弹出窗口,当胡佛请求服务器的数据显示。但是,我可以阻止多个弹出窗口的唯一方法是使用同步ajax。我知道同步ajax应该很少,如果永远不会被使用。这可以异步完成吗?我只是在学习需要回调,并且感觉它们是相关的。感谢
(function( $ ){
$.fn.screenshotPreview = function() {
xOffset = 20;
yOffset = 10;
this.hover(function(e) {
$.ajax({
url: 'getPopup.php',
success: function(data)
{
$("body").append('<div id="screenshot">dl><dt>Name:</dt><dd>'+data.name+'</dd><dt>User Name:</dt><dd>'+data.username+'</dd></dl></div>');
$("#screenshot")
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px")
.fadeIn("fast");
},
async: false,
dataType: 'json'
});
},
function() {
$("#screenshot").remove();
});
this.mousemove(function(e) {
$("#screenshot").css("top",(e.pageY - yOffset) + "px").css("left",(e.pageX + xOffset) + "px");
});
};
})( jQuery );
答案 0 :(得分:1)
您想要添加一个标记,说明您是否已开始显示弹出窗口:
(function( $ ){
var showing = false;
$.fn.screenshotPreview = function() {
xOffset = 20;
yOffset = 10;
this.hover(function(e) {
if(!showing){
showing = true;
$.ajax({
url: 'getPopup.php',
success: function(data)
{
if(showing){
$("body").append('<div id="screenshot">dl><dt>Name:</dt><dd>'+data.name+'</dd><dt>User Name:</dt><dd>'+data.username+'</dd></dl></div>');
$("#screenshot")
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px")
.fadeIn("fast");
}
},
dataType: 'json'
});
}
},
function() {
showing = false;
$("#screenshot").remove();
});
this.mousemove(function(e) {
$("#screenshot").css("top",(e.pageY - yOffset) + "px").css("left",(e.pageX + xOffset) + "px");
});
};
})( jQuery );