Implement the method of making the program wait for a period of time

JAVA background time waiting

JAVA implements the method of making the program wait for a period of time. If you want the code to wait for a period of time before continuing to execute in JAVA, you can sleep the current thread for a period of time.

1. sleep method through thread

try {
    Thread.currentThread().sleep(1000);//Wait 1 second
} catch (InterruptedException e) {
	e.printStackTrace();
}

Add this statement where the program needs to wait to make the program wait. The parameter 1000 here is in milliseconds, that is, this statement can make the program wait for 1 second.

2. Pass thread sleep

try {
	for(int i=1;i<6;i++) {
		Thread.sleep(2000);//Wait 2 seconds
	}
} catch (InterruptedException e){
	e.printStackTrace();
}

3. sleep method in timeunit class

TimeUnit.DAYS.sleep(1);//day
TimeUnit.HOURS.sleep(1);//hour
TimeUnit.MINUTES.sleep(1);//branch
TimeUnit.SECONDS.sleep(1);//second
TimeUnit.MILLISECONDS.sleep(1000);//millisecond
TimeUnit.MICROSECONDS.sleep(1000);//subtle
TimeUnit.NANOSECONDS.sleep(1000);//nanosecond

try {
	for(int i=1;i<6;i++) {
		TimeUnit.SECONDS.sleep(3);//Wait 3 seconds
	}
} catch (InterruptedException e){
	e.printStackTrace();
}

The method provided by TimeUnit class actually calls the sleep method of Thread class at the bottom to make the program wait. However, it is encapsulated in the upper layer according to the time unit. As shown in the figure above, there are seven options, which can easily select the time unit you need for use.

reference resources: https://www.cnblogs.com/pcheng/p/7099385.html

html page waiting time

1. Realized by html meta

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Relative path -->
<meta http-equiv="Refresh" content="3,url=IndexServlet">
<title>Jump...</title>
</head>
<body>
 
</body>
</html>

Interpretation: in content="3,url=IndexServlet", 3 refers to a delay of 3 seconds.

2. Through setInterval

Through the setInterval function, the countdown time is updated periodically and updated to the page at the same time. The display effect on the page is 3 2 1, and then jump to index html

<span id="remainSeconds">3</span>
<script type="text/javascript">
    setInterval(jump,1000);
    var sec = 3;
    function jump(){
        sec--;
        if(sec > 0){
            document.getElementById('remainSeconds').innerHTML = sec;
            
        }else{
            window.location.href = 'index.html';
        }
    }
</script>

Execute a delay function through setTimeout to achieve the purpose of jump

setTimeout(jump,3000);
function jump(){
	window.location.href='IndexServlet';
}

3. Realized through setTimeout

Through the setTimeout function, the countdown time is updated periodically and updated to the page at the same time. The display effect on the page is 3 2 1, and then jump to index html

<span id="remainSeconds">3</span>
<script type="text/javascript">
    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);
</script>

reference resources: https://blog.csdn.net/kt1776133839/article/details/101036384

Tags: Java Javascript

Posted by XTTX on Wed, 25 May 2022 19:32:54 +0300