java 等待窗口_实现让程序等待一段时间的方法

JAVA后台时间等待

JAVA中实现让程序等待一段时间的方法,JAVA中想让代码等待一段时间再继续执行,可以通过让当前线程睡眠一段时间的方式。

1.通过线程的sleep方法

try {

Thread.currentThread().sleep(1000);//等待1秒

} catch (InterruptedException e) {

e.printStackTrace();

}

在需要程序等待的地方加入这个语句,实现让程序等待,这里的参数1000是以毫秒为单位,即这语句可以让程序等待1秒。

2.通过Thread.sleep

try {

for(int i=1;i<6;i++) {

Thread.sleep(2000);//等待2秒

}

} catch (InterruptedException e){

e.printStackTrace();

}

3.TimeUnit类里的sleep方法

TimeUnit.DAYS.sleep(1);//天

TimeUnit.HOURS.sleep(1);//小时

TimeUnit.MINUTES.sleep(1);//分

TimeUnit.SECONDS.sleep(1);//秒

TimeUnit.MILLISECONDS.sleep(1000);//毫秒

TimeUnit.MICROSECONDS.sleep(1000);//微妙

TimeUnit.NANOSECONDS.sleep(1000);//纳秒

try {

for(int i=1;i<6;i++) {

TimeUnit.SECONDS.sleep(3);//等待3秒

}

} catch (InterruptedException e){

e.printStackTrace();

}

TimeUnit类提供的方法,其实底层调用的也是Thread类的sleep方法,让程序进行等待。只不过他在上层根据时间单位进行了封装,如上图,共有7种可以选择,可以方便的选择自己需要的时间单位进行使用。

html页面时间等待

1.通过html meta实现

跳转...

释义:content="3,url=IndexServlet" 中3是指延迟3秒。

2.通过setInterval实现

通过setInterval函数,来周期性的更新倒计时间,同时更新到页面。页面上的显示效果是3 2 1,然后跳转到index.html

3

setInterval(jump,1000);

var sec = 3;

function jump(){

sec--;

if(sec > 0){

document.getElementById('remainSeconds').innerHTML = sec;

}else{

window.location.href = 'index.html';

}

}

通过setTimeout执行一个延迟函数来达到跳转的目的

setTimeout(jump,3000);

function jump(){

window.location.href='IndexServlet';

}

3.通过setTimeout实现

通过setTimeout 函数,来周期性的更新倒计时间,同时更新到页面。页面上的显示效果是3 2 1,然后跳转到index.html

3

var sec = 3;

function jump(){

sec--;

if(sec > 0){

document.getElementById('remainSeconds').innerHTML = sec;

setTimeout(this.jump,1000);

}else{

window.location.href = 'index.html';

}

}

setTimeout(jump,1000);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值