(function () { var div, txt, timer; div = document.getElementById('charcount'); txt = document.getElementById('inputfield'); txt.onkeyup = function () { clearTimeout(timer); timer = setTimeout(function () { div.innerHTML = txt.value.length; }, 100); }; txt.focus(); })();