0

使用来自https://github.com/thorst/jquery-idletimer的可爱的 Jquery idletimer :似乎我无法将空闲计时器附加到特定元素(而不是整个文档)。这是我非常简单的代码:

要触发空闲计时器:

$("#testme"  ).on( "idle.idleTimer", function(event, elem, obj){ $("#test").html("now idle");});
$( "#testme" ).on( "active.idleTimer", function(event, elem, obj, triggerevent){ $("#test").html("not idle");});
$( "#testme" ).idleTimer(3000);

要在文档中调用它:

<div id="testme" style="border: solid;"><p id="test">I am idle</p></div>

事情就是这样。如果我用 $(document) 替换 $("#testme" ) 它工作正常。但不是当它附加到 div 元素时。我正在使用 jquery-1.11.0.js、jquery.mobile-1.4.2 和 idle-timer.js。

更新:我现在在http://jsfiddle.net/peterrr73/C3w7f/有一个 jsFiddle,它显示了这个半工作,附加到一个元素。正如小提琴所示:空闲的火;然后主动火灾 - 但由于某种原因它停止了。感谢 Jeromy French 对 jsFiddle 的帮助;他在http://jsfiddle.net/jhfrench/2aGL4/12/上处理了整个文档,但不是一个元素。

4

1 回答 1

0

这现在正在工作.. 请参阅http://jsfiddle.net/peterrr73/C3w7f/3,有两个单独的 div 元素,每个元素仅响应该 div 中的事件:

<div id="testme" style="border: solid;"><p id="test">I am idle</p></div>
<div id="testme2" style="border: solid;"><p id="test2">I am idle</p></div> 

(function ($) {    
   $("#testme"  ).on( "idle.idleTimer", function(event, elem, obj){ $("#test").html("I am very idle");});
   $( "#testme"  ).on( "active.idleTimer", function(event, elem, obj, triggerevent){ $("#test").html("not idle");});
   $( "#testme" ).idleTimer(3000);
   $("#testme2"  ).on( "idle.idleTimer", function(event, elem, obj){ $("#test2").html("I am very idle");});
   $( "#testme2"  ).on( "active.idleTimer", function(event, elem, obj, triggerevent){ $("#test2").html("not idle");});
    $( "#testme2" ).idleTimer(3000);
 })(jQuery);

不知道为什么它不起作用......如果将计时器语句包装在 (function ($) { })(jQuery); 中,它都可以正常工作。如果你不这样做。

于 2014-03-27T13:34:02.637 回答