Jquery: observe field with frequency

I need to use jQuery to detect changes in a text file, so some ajax is called.

The trick is that I really don't want to bind this action to blurring or keyup events; The first is too deaf for the user, and the second is too "average": the user enters new keys before ending the ajax call, so the user interface is small (so the answer to this SO question is not applicable in my case).

I need a code that checks if a field value has changed, but periodically (i.e. every 0.5 seconds). I would like to use the same code in several fields, calling different ajax functions with different frequencies, so reuse here is really a plus.

In Rails, I had observe_field(internally uses Prototype Form.Element.observer , I think) that did just that. How can I accomplish the equivalent thing in jQuery?

+3
source share
3 answers

I finished my own jquery plugin.

It is (freely) based on a microprocessor implementation.

Created a github project for it:

http://github.com/splendeo/jquery.observe_field

Main differences of Mic:

  • I use jQuery methods instead of my own in a couple of places.
  • I do not activate / deactivate onblur and onfocus detection. That way, I can track changes made using code (rails "register_field" does this, and I wanted to simulate it).
  • reset . , , iphone.

:

$(function() {
  // Executes a callback detecting changes with a frequency of 1 second
  $("#item1").observe_field(1, function( ) {
    alert('Change observed! new value: ' + this.value );
  });
});
+5

, JS: html ?
$(...). Focus (...), jquery, .

onfocus , , , "".
, onblur .

, , / / .

+2
0

Source: https://habr.com/ru/post/1734023/


All Articles