只若初见

3秒后跳转

电脑版发表于:2020/6/19 15:26

实现一个简单的3秒后跳转

html:

注册成功!<span style="margin-left:5px;display:inline-block">
<span id="showtime">3</span>秒后跳转</span>
<a style="margin-left:5px" href='/login/index'>立即登录</a>

js:

var showtime = document.getElementById("showtime");
var showinfo = document.getElementById("showinfo");
var lastsecond = 2;

var lastsecondInterval = setInterval(function () {

    if (lastsecond <= 0) {
        clearInterval(lastsecondInterval);
        location.href = "/login/index";
    }
    showtime.innerText = lastsecond;
    lastsecond--;
}, 1000)

其实上面这种做法不是太好,应该先减,在赋值,这样科学一点。而不是先赋值在减这样就会慢一步。

可以参考一下这样,比如发送一个验证码后提示n秒有效:

var lasttime = 300;
$("#checkcodediv").show();
$("#checktip").html(lasttime + "秒有效");
var lasttimeInt = setInterval(function () {
    //先减
    lasttime--;
    //在赋值
    $("#checktip").html(lasttime + "秒有效");
    if (lasttime == 0) {
        $("#checktip").html("验证码已经失效");
        clearInterval(lasttimeInt);
    }
}, 1000);




关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢