7

I have a list of links which point to html pages.

<ul id="item-list">
    <li><a href="assets/data/item1.html">Item 1</a></li> 
    <li><a href="assets/data/item2.html">Item 2</a></li>
    <li><a href="assets/data/item3.html">Item 3</a></li>
    <li><a href="assets/data/item3.html">Item 4</a></li>
</ul>

And I have a JavaScript(jQuery) which receives and appends the html to my document.

var request;
$('#item-list a').live('mouseover', function(event) {
    if (request)
        request.abort();
        request = null;
    
    request = $.ajax({
        url: $(this).attr('href'),
        type: 'POST',
        success: function(data) {
            $('body').append('<div>'+ data +'</div>')
        }
    });
});

I've tried to work with setTimeout() but it does not work as I expected.

    var request, timeout;
$('#item-list a').live('mouseover', function(event) {
    timeout = setTimeout(function(){
        if (request)
            request.abort();
            request = null;

        request = $.ajax({
            url: $(this).attr('href'),
            type: 'POST',
            success: function(data) {
                $('body').append('<div>'+ data +'</div>')
            }
           });
        }, 2000
    );
});

How can I tell jQuery to wait (500ms or 1000ms or …) on hover before sending the new request?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
gearsdigital
  • 13,915
  • 6
  • 44
  • 73
  • 1
    Am I right in thinking you want to make the request only if the user hovers over the link for more than 2 seconds? – lonesomeday Oct 19 '10 at 21:44

4 Answers4

8

I think that perhaps instead of aborting the request, you should control the ajax requests with a variable, for example, called processing=false, that would be reset to false, in the success/error function. Then you would only execute the function in setTimeout, if processing was false.

Something like:

var request, timeout;
var processing=false;
$('#item-list a').live('mouseover', function(event) {
  timeout = setTimeout(function() {
    if (!processing) {
      processing=true;
      request = $.ajax({
        url: $(this).attr('href'),
        type: 'POST',
        success: function(data) {
          processing=false;
          $('body').append('<div>'+ data +'</div>')
        }
      });
    }
  }, 2000);
});
w3spi
  • 4,380
  • 9
  • 47
  • 80
netadictos
  • 7,602
  • 2
  • 42
  • 69
2
$.fn.extend( {
        delayedAjax: function() {
          setTimeout ($.ajax, 1000 );
        }
});

  $.fn.delayedAjax();

Seems to work but probably not the prettiest solution. Also, you need to add some code to pass the args & the timeout val if you want

jellyfishtree
  • 1,811
  • 1
  • 10
  • 11
2

You will need to have a variable that can act a countdown timer if you will, that a mouseout event will cancel as well...

$(function(){
  $("#item-list a").live("mouseover",function(){
    var a = $(this);
    a.data("hovering","1");
    setTimeout(function(){
        if (a.data("hovering") == "1") {
            // this would actually be your ajax call
            alert(a.text());
        }
    }, 2000);
  });
  $("#item-list a").live("mouseout",function(){
    $(this).data("hovering","0");
  });
});
Goyuix
  • 23,614
  • 14
  • 84
  • 128
-2

this works for me...

$(show_id).animate({
  opacity: 0
}, 5000, function() {
  $(show_id).html(data)
});
Cali
  • 1
  • 4
    Thanks for offering a suggestion. I recommend that you 1. Explain your answer further and 2. Tie it to the original question. First, I'm surprised to see you using jQuery animation- are you suggesting that you have to animate in the UI to achieve the result? Second, shouldn't there be an ajax call? – Jacob Foshee Sep 03 '13 at 23:21