Skip to main content
 首页 » 编程设计

javascript之悬停时停止计时器

2025年05月04日12thcjp

我无论如何都不是 JS 编码员。我知道足以让事情做我想做的事,但无法从头开始编码。我的问题是:

我们有一个购物车,当您添加产品时,购物车会显示 4 秒,除非客户将鼠标悬停在购物车上。当光标悬停在它上面时,我似乎无法让它停止超时。

$(document).ready(function () { 
    setTimeout(function () { $('#ctl00_ctl00_ctlHeader_divOrderProducts').hide(); }, 4000); 
}); 

请您参考如下方法:

存储返回 setTimeout()在一个变量中,并将其用于 clearTimeout() :

// t is a global scope variable. 
// Probably a good idea to use something better than 't' 
var t; 
$(document).ready(function () { 
  // Store the return of setTimeout() 
  t = setTimeout(function () { $('#ctl00_ctl00_ctlHeader_divOrderProducts').hide(); }, 4000); 
}); 
 
$('cart-selector').hover(function() { 
   if (t) { 
    // Call clearTimeout() on hover() 
    clearTimeout(t); 
   } 
});