JQuery delayedObserver plugin

Posted on January 6, 2010

0


I’ve never liked javascript programming. I’ve done some efforts, but found that its just as boring as it is hard.

Well, you can’t get around it, can you..?

I was fiddling with some ajax prototyping for a searchfield the other day. You know, the stupidly basic inputfield onchange=”callsomefunction()” stuff… Or was it onkeypress=”callsomefunction()”?

Luckily, a Stack Overflow question on this subject led me to JQuery Utils and the delayedObserver plugin.

Below a very simple example. An a delayedObserver callback function (“testfunction”) is set to the input field, with 1 second delay. This means that one second after finishing writing in the text field, the callback function (testfunction()) is executed. Simple and elegant!

<input type="text" id="input1" />

<script type="text/javascript">
	$(document).ready(function(){
                // Register the callback function with 1 sec delay
		$('input#input1').delayedObserver(testfunction, 1);
	});
	//******************************************************
	// Callback function
        function testfunction(value, object) {
		alert($('input#input1').val());
	}
</script>

Thank you, haineault!

Advertisements
Tagged: ,
Posted in: JQuery