繁体   English   中英

没有同步ajax的弹出窗口

[英]Popup without synchronous ajax

我有一个弹出窗口,当鼠标悬停时,它会请求服务器显示数据。 但是,我可以阻止多个弹出窗口的唯一方法是使用同步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 );

您想添加一个标志来表明您是否已经开始显示弹出窗口:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM