参考: OnKeyUp JavaScript Time Delay?var typewatch = function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    }  
}();调用方法一window.onload = function () {
    document.getElementById('domain').onkeyup = function() {
        typewatch(function(){alert('Time elapsed!');}, 1000 );
    };
};调用方法二<input type="text" name="domain" id="domain"
   onKeyUp="typewatch(function(){alert('Time elapsed!');}, 1000 );"/>

解决方案 »

  1.   

    试着写了一下,看看 是不是想要的。思路大体都是一致的。<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>test</title>
    </head>
    <body>
    <input type="text" name="" id="txt"/>
    <script type="text/javascript">
        var txt = document.getElementById('txt');
        var timer = null;
        txt.onkeyup = function(){
            var val = this.value;
            timer && clearTimeout(timer);
            timer = setTimeout(function(){
                check(val);
            }, 1000);
        }    function check(val){
            if(/^\d+$/.test(val)){
                alert('输入合法!');
            } else {
                alert('请输入数字!');
            }
        }
    </script>
    </body>
    </html>