Skip to content

Instantly share code, notes, and snippets.

@iancanderson
Created April 8, 2011 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancanderson/909312 to your computer and use it in GitHub Desktop.
Save iancanderson/909312 to your computer and use it in GitHub Desktop.
new jquery plugin in progress to support www.pilvinotes.com
(function( $ ){
$.fn.pollForChanges = function( interval, changedCallback ) {
// this maps element ids to corresponding values
var _mapIdsToValues = {};
// skip if not a textarea or an text input (for now)
var inputs = this.filter("textarea,input[type='text']");
setInterval( function() {
var aryIdsChanged = [];
// figure out which elements' values changed
inputs.each(function() {
var elementId = this.id;
// get the new value of this element
var newValue = $("#" + elementId).val();
// add id to array if value changed
if (newValue !== _mapIdsToValues[elementId]) {
aryIdsChanged.push(elementId);
}
// store the new value
_mapIdsToValues[elementId] = newValue;
});
changedCallback(aryIdsChanged);
}, interval);
};
})( jQuery );
<html>
<head>
<title>test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="jquery.pollForChanges.js"></script>
</head>
<body>
<input type="text" class="pollForChanges" id="tagList"></input>
<textarea class="pollForChanges" id="noteContent" rows="20" cols="60"></textarea>
<p id="status"></p>
<script type="text/javascript">
$(function() {
f1 = function( aryIdsChanged ) {
$.each(aryIdsChanged, function(index, value) {
$("#status").prepend(value + " <strong>changed!</strong><br/>");
});
}
$(".pollForChanges").pollForChanges(1000, f1);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment