0

I am deeply puzzled...

A day or so ago I posted a query about the use of idletimer attached to an element, so that events would or would not fire on activity/absence of activity in a particular element on a page, as follows:

$("#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);

It seemed I had got this working: you can see it operating happily in http://jsfiddle.net/peterrr73/C3w7f/3. But when I paste this same code into a separate file and put it on the web it fails: you can see this at http://www.sd-editions.com/CantApp/index4.html. To make matters more puzzling: you can see at http://www.sd-editions.com/CantApp/index2.html that the idler is working when attached to the whole document.

What is going on? There are no errors reported when I go the online not-functioning instance.

Peter Robinson
  • 363
  • 4
  • 17

1 Answers1

1
  • jsfiddler runs onLoad, so all body nodes are loaded
  • First link uses dom elements which are not loaded
  • Second link attaches events to the document

Here is your solution (wrap code with selectors):

$(function() {
/* your code goes here */
});
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • Yep, just figured this! An alternative is to place the script after the closing body tag, so that the whole body with all its elements loads up before adding the timer to the elements. It all makes perfect sense now. This is why the timer worked on document, but not on elements -- the document was there, so no trouble, but the elements were not. – Peter Robinson Mar 27 '14 at 16:56