逐个删除字符

4

我遇到了一个问题。我正在为自己制作一个博客,一切都很顺利。但有一天我想用我的登录输入框做点酷炫的事情,那么如果我能够逐个删除占位字符会怎样呢?

var run = 0;
var inte;
function removet(obj) {
    run = 0;
    setInterval(function () {
        if(run > 8) {
            clearInterval();
        }
        else {
            stri = obj.placeholder;
            stri = stri.substring(0, stri.length - 1);
            obj.placeholder = stri;
            run++;
        }
    }, 22, obj);
}

嗨,它能工作了!但是我遇到了一个小问题。这是我的HTML代码:

<input type="text" placeholder="Username" onblur="this.placeholder = 'Username'" onfocus="removet(this)">
<input type="password" placeholder="Password" onblur="this.placeholder = 'Password'" onfocus="removet(this)">
<button type="submit" id="login-button">Login</button>

删除用户名的操作正常,但是当我尝试删除密码时……两者都只删除了一半。为什么?我的错误在哪里?


@Teemu 我也在想同样的事情,但是我脑海中的描述不够清晰。把它作为一个答案吧! - autistic
1个回答

1
尝试不要清除所有间隔。
function removet(obj) {
    run = 0;
    var k= setInterval(function () {
        if(run > 8) {
            clearInterval(k);
        }
        else {
            stri = obj.placeholder;
            stri = stri.substring(0, stri.length - 1);
            obj.placeholder = stri;
            run++;
        }
    }, 22, obj);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接