1. 程式人生 > >Atitit 定時器timer 總結 目錄 1. 定時器 迴圈定時器 和timeout超時定時器 1 2. Spring定時器 1 2.1. 大概流程 1 2.2. 核心原始碼springboot 1

Atitit 定時器timer 總結 目錄 1. 定時器 迴圈定時器 和timeout超時定時器 1 2. Spring定時器 1 2.1. 大概流程 1 2.2. 核心原始碼springboot 1

Atitit 定時器timer 總結

 

目錄

1. 定時器 迴圈定時器 和timeout超時定時器 1

2. Spring定時器 1

2.1. 大概流程 1

2.2. 核心原始碼springboot 1

3. Js定時器 window.setInterval 2

4. Java定時器 timer 3

 

 

  1. 定時器 迴圈定時器 和timeout超時定時器

 

  1. Spring定時器

 

    1. 大概流程

增加一個定時配置類,新增@Configuration和@EnableScheduling註解

使用cron表示式生成器生成一個表示式

定義一個方法,增加Scheduled註解,講表示式放入即可

執行此springboot專案即可。

 

 

    1. 核心原始碼springboot

package timer;

 

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

 

@Configuration

@EnableScheduling

public class SchedulingConfig {

 

    @Scheduled(cron = "0/5 * * * * ? ")

    public void test(){

        System.out.println("定時排程。。。。。。。。。。。。");

    }

    

}

 

注意,實際的時間他說只能六個引數。。表示式生成器是7個引數,去掉最後一個即可

 

  1. Js定時器 window.setInterval

 

1.迴圈執行:

var timeid = window.setInterval(“方法名或方法”,“延時”);

window.clearInterval(timeid);

<script type="text/javascript">

  $(document).ready(function(){

    //迴圈執行,每隔1秒鐘執行一次 1000     var t1=window.setInterval(refreshCount, 1000);

    function refreshCount() {

      console.log("ready");

    }

    //去掉定時器的方法  
    window.clearInterval(t1);   
 });
</script>

 

  1. Java定時器 timer

 

 

 16     Timer timer = new Timer();

17     timer.schedule(new TimerTask() {

18       public void run() {

19         System.out.println("-------設定要指定任務--------");

20       }

21     }, 2000);// 設定指定的時間time,此處為2000毫秒

 

 

 

 

Atitit spring 定時器 CRON表示式    含義