开发者

ajax request once sent on mouseover, response recieved, but should not be sent again when i mouseover on the html element again

i am sending an ajax request on a mouse over event and I am receiving the desired response. But when i send again hover on the element whose request was sent, the request is sent again.

i do not want to send the request again, rather i want the page to use the response previously received.

Who to do it?? here is a sample code. - > this function is being called on mouse over, do not want to send request again on the same mouse over element.

function showImage(val)
{
    $("#DIV").html('<IMAGESOURCE="../adsd/ajax_loader.gif">');

    $.ajax({
        type        : "get",
        cache   : false,
        url     : "blabla.php?imgID="+val,
        data        : $(this).serializeArray(),
        success: function(data) {
            document.getElementById("DIV").innerHTM开发者_如何学PythonL = data;
        }
    });
};


Set a variable to a value when the response is received and check for that variable before send ing the request.

var response = false;
function showImage(val)
{
    $("#DIV").html('');
    if (response == false) {
        $.ajax({    
            type    : "get",
            cache   : false,
            url     : "blabla.php?imgID=" + val,
            data    : $(this).serializeArray(),
            success: function(data)
            {
                document.getElementById("DIV").innerHTML = data;
                response = true;
            }            
        });
    } else {
        // What to do if the request was sent before
    }
};


What I think is you need to use jQuery.data() on mouseover do

if(!jQuery.data(dom, 'sent')){
  //take action
  jQuery.data(dom, 'sent', true);// do it on success
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜