簡體   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