• JS定时器实现页面N秒后跳转 实现每隔 1s 自动刷新页面并格式化的显示当前时间


    1. 通过 setInterval 函数,来周期性的更新倒计时间,同时更新到页面。即通过设置页面可以显示 3 2 1,然后跳转。1000指的是每隔1秒执行一次。

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <div class="one">div>
    9. <script>
    10. let timer = 3; // 设置跳转时间
    11. let div = document.querySelector('.one');
    12. setInterval(function() {
    13. if(timer==0) {
    14. location.href = "http://www.baidu.com"; //跳转目的地
    15. } else {
    16. div.innerHTML = "您将在"+timer+"秒后跳转到百度";
    17. timer--;
    18. }
    19. },1000); // 1000毫秒 一秒执行一次
    20. script>
    21. body>
    22. html>

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

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <h3>将在3秒后跳转到百度页面h3>
    9. <script>
    10. setTimeout(function() {
    11. window.location.href = "http://www.baidu.com";
    12. },3000); // 3000毫秒 在这设置跳转时间
    13. script>
    14. body>
    15. html>

     


    3. SimpleDateFormat

    1. //第一种
    2. @WebServlet("/aaa")
    3. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    4. req.setCharacterEncoding("utf8");
    5. resp.setContentType("text/html;charset=utf8");
    6. // 设置刷新时间为一秒
    7. resp.setHeader("Refresh","1");
    8. // 格式化日期
    9. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这种是24小时制
    10. //DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //这种是12小时制
    11. String format1 = format.format(new Date());
    12. resp.getWriter().write("当前时间 "+format1);
    13. }
    14. //第二种
    15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    16. resp.setContentType("text/html; charset=utf-8");
    17. // 设置Refresh时间为1秒
    18. resp.setHeader("Refresh", "1");
    19. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
    20. String time = sdf.format(new Date());
    21. resp.getWriter().write("当前时间:" + time);
    22. }

     或者

    1. @WebServlet("/aaa")
    2. public class test extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. resp.setContentType("text/html; charset=utf-8");
    6. // 设置Refresh时间为1秒
    7. resp.setHeader("Refresh", "1");
    8. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    9. String time = sdf.format(new Date());
    10. resp.getWriter().write("当前时间:" + time);
    11. }
    12. }

  • 相关阅读:
    MySQL锁与死锁分析
    前端基础(四十二):SVG入门
    【Qt学习】第一个Qt Quick程序
    Debezium系列之:将Debezium的info日志、warn日志、error日志拆分到不同文件中
    Flutter 小计1
    17个 Python常见错误
    Nodejs 如何开启多进程,进程如何通讯
    SmartX 边缘计算解决方案:简单稳定,支持各类应用负载
    【运维面试100问】(一)打包了一个镜像推送上去harbor,推不上是什么原因?
    ubuntu18.04安装mysql5.7并配置数据存储路径
  • 原文地址:https://blog.csdn.net/m0_73381672/article/details/134253315