简体   繁体   English

jQuery setTimeout关于它的几个问题

[英]jquery setTimeout few questions about it

i have a website with tags system like kind of the system that work on stackoverflow. 我有一个带有标签系统的网站,就像在stackoverflow上工作的那种系统。 my questions about it is: 我对此的疑问是:

    $("#area1 ul li").hover(function(){
        var width= $(this).offset();
        $(".flyout").css({"top":width.top+19,"left":width.left});
        $(".flyout").toggle();
        setTimeout(function() {
          $.ajax({ 
            url: web + "sources/queans/sql.php", type: "POST",
            data: { 
               action: "taginfo",
               tag: $(this).text(),
               token: t, 
               ajax: "1" 
            },
            success: function (output) {
              $(".flyout").html(output);
            }
          });
        }, 2000);
     $(".flyout").html('<center><img style="margin-top:20px;"  src="http://www.cloudynights.com/public/style_images/master/ajax_loading.gif" /> </center>');
   });
  1. Do this Jquery script is wait 2 seconds while the mouse are hover the element? 鼠标悬停元素时,此Jquery脚本是否要等待2秒?
  2. if user remove the mouse hover the element do the query will still running and execute the code? 如果用户将鼠标悬停在该元素上,查询是否仍将运行并执行代码? if no how can i stop the code before it require from sql.php file data? 如果没有,如何在sql.php文件数据要求之前停止代码?

Do this Jquery script is wait 2 seconds while the mouse are hover the element? 鼠标悬停元素时,此Jquery脚本是否要等待2秒?

Not exactly, once a user has hovered over the element, a timer is started and 2 seconds later the action is performed. 不完全是,一旦用户将鼠标悬停在元素上,就会启动计时器,并在2秒后执行操作。 The user does not have to remain hovering the element for this to happen. 用户不必为此而将鼠标悬停在元素上。

if user remove the mouse hover the element do the query will still running and execute the code? 如果用户将鼠标悬停在该元素上,查询是否仍将运行并执行代码?

As specified above, the action will execute 2 seconds after first hovering the element, regardless of what the user does thereafter. 如上所述,该操作将在第一次将元素悬停2秒钟后执行,无论用户随后如何操作。

if no how can i stop the code before it require from sql.php file data? 如果没有,如何在sql.php文件数据要求之前停止代码?

Capture the result of the call to setTimeout into a variable (commonly called timerId or similar), and call clearTimeout(timerId) when the user stops hovering the element. 将对setTimeout的调用结果捕获到一个变量(通常称为timerId或类似名称)中,并在用户停止悬停元素时调用clearTimeout(timerId)

See the following simplified demo. 请参阅以下简化的演示。

 var timerId; $('.container').hover(function(){ $('.message').text('Hover has been started, background will change in 5 seconds. Mouse out to cancel'); var $this = $(this); timerId = setTimeout(function(){ $this.css('background-color','red'); },5000); }, function(){ clearTimeout(timerId); $('.message').text('Action cancelled'); }); 
 .container{ width:300px; height:300px; border: 1px solid black } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div>Hover over me</div> <div class="message"></div> </div> 

.hover() usually takes two arguments, handlerIn and handlerOut functions. .hover()通常.hover()两个参数, handlerInhandlerOut函数。 As you have only one function, it will be invoked when the mouse pointer enters the element . 由于您只有一个函数,因此当mouse pointer enters the element时将调用该函数。 My understanding is that you're displaying some pop-up next to element with some loading icon on hover, that's good because giving some visual feedback to the user encourages him to stay on that element. 我的理解是,您正在元素旁边显示一些弹出窗口,并在悬停时带有一些加载图标,这很好,因为向用户提供一些视觉反馈会鼓励他停留在该元素上。

setTimeout(callback,2000) will invoke that callback after waiting for minimum 2sec (it can be more - no guarantee on that ;)) but if the user leaves the element you're still firing the Ajax call without tracking that mouseleave event. setTimeout(callback,2000)将在等待至少2秒后调用该回调(可以做更多-不能保证;)),但是如果用户离开该元素,则您仍会在不跟踪mouseleave事件的情况下触发Ajax调用。 So pass another function to hover() which will be invoked when the user leaves the element. 因此,将另一个函数传递给hover() ,该函数将在用户离开元素时被调用。

/* two variables outside hover scope to 
   hold reference to setTimeout & Ajax call*/
var timer, ajaxRequest;
$("#area1 ul li").hover(function(){
        var width= $(this).offset();
        $(".flyout").css({"top":width.top+19,"left":width.left});
        $(".flyout").toggle();
       timer = setTimeout(function() {
         ajaxRequest = $.ajax({ 
            url: web + "sources/queans/sql.php", type: "POST",
            data: { 
               action: "taginfo",
               tag: $(this).text(),
               token: t, 
               ajax: "1" 
            },
            success: function (output) {
              $(".flyout").html(output);
            }
          });
        }, 2000);
     $(".flyout").html('<center><img style="margin-top:20px;"  src="http://www.cloudynights.com/public/style_images/master/ajax_loading.gif" /> </center>');
   }, 
  function(){
    // second callback to handle mouseleave
    if(typeof ajaxRequest !== 'undefined'){
        // abort the ongoing Ajax call
        ajaxRequest.abort();
    }else if(typeof timer !== 'undefined'){
       // timeout callback is not invoked yet, so clear it
       clearTimeout(timer);
    }
   // show some message in flyout or close the popup directly
  });

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

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