JavaScript等待一定时间后执行方法


<input id="otxt" type="text" />
<script>
function onSub(){
 alert('验证');
}
var timer =null
document.getElementById('otxt').onkeyup =function(){
 clearTimeout(timer);
 timer = setTimeout(onSub, 2000);
}
</script>