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 );
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签