1. 通过 setInterval 函数,来周期性的更新倒计时间,同时更新到页面。即通过设置页面可以显示 3 2 1,然后跳转。1000指的是每隔1秒执行一次。
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div class="one">div>
- <script>
- let timer = 3; // 设置跳转时间
- let div = document.querySelector('.one');
- setInterval(function() {
- if(timer==0) {
- location.href = "http://www.baidu.com"; //跳转目的地
- } else {
- div.innerHTML = "您将在"+timer+"秒后跳转到百度";
- timer--;
- }
- },1000); // 1000毫秒 一秒执行一次
- script>
- body>
- html>
2. 通过 setTimeout 执行一个延迟函数来达到跳转的目的。
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <h3>将在3秒后跳转到百度页面h3>
- <script>
- setTimeout(function() {
- window.location.href = "http://www.baidu.com";
- },3000); // 3000毫秒 在这设置跳转时间
- script>
- body>
- html>
3. SimpleDateFormat
- //第一种
- @WebServlet("/aaa")
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("utf8");
- resp.setContentType("text/html;charset=utf8");
- // 设置刷新时间为一秒
- resp.setHeader("Refresh","1");
- // 格式化日期
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这种是24小时制
- //DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //这种是12小时制
- String format1 = format.format(new Date());
- resp.getWriter().write("当前时间 "+format1);
- }
-
-
-
-
- //第二种
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.setContentType("text/html; charset=utf-8");
- // 设置Refresh时间为1秒
- resp.setHeader("Refresh", "1");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
- String time = sdf.format(new Date());
- resp.getWriter().write("当前时间:" + time);
- }
或者
- @WebServlet("/aaa")
- public class test extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.setContentType("text/html; charset=utf-8");
- // 设置Refresh时间为1秒
- resp.setHeader("Refresh", "1");
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(new Date());
- resp.getWriter().write("当前时间:" + time);
- }
- }
