Using Delay on a textbox
I am trying to build a functionality where a user t开发者_StackOverflow中文版ypes text in the textbox and after a delay of 1 second, each character typed is converted to its upper case. This should happen as the user types.
Can i do using jquery?
This might not be the most jQuery-esque way to do it, but something like this.
setInterval((function (textbox) {
return function () {
textbox.val(textbox.val().toUpperCase());
}
}($("#myTextBox"))), 1000);
Of course changing $("#myTextBox")
to the relevant selector
Check out the answer to this question for more detail:
How to delay the .keyup() handler until the user stops typing?
But here is a full working example of it all tied together:
$(document).ready(
function()
{
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$("#testerText").keyup(
function()
{
delay(function()
{
$("#testerText").val($("#testerText").val().toUpperCase());
}, 1000);
});
});
http://jsfiddle.net/5Nq2f/
精彩评论