[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
js中定时器有两种,一个是循环执行 setInterval,另一个是定时执行 setTimeout。
注意:定时器需要在页面销毁的时候清除掉,不然会一直存在!
1. 循环执行( setInterval )
顾名思义,循环执行就是设置一个时间间隔,每过一段时间都会循环执行这个方法,直到这个定时器被销毁掉;
语法:
setInterval(code, milliseconds); setInterval(function, milliseconds, param1,
param2, ...); code/function 必需。要调用一个代码串,也可以是一个函数。 milliseconds 必须。周期性执行或调用
code/function 之间的时间间隔,以毫秒计。 param1, param2, ... 可选。 传给执行函数的其他参数(IE9
及其更早版本不支持该参数)。 返回值: 返回一个 ID(数字),可以将这个ID传递给 clearInterval() 来取消执行。
eg:
开始的时候创建了一个定时器 setInterval ,时间间隔为2秒,每2秒都会调用一次函数 valChange,从而使 value 的值+1。
<template> <div> <h1>{{value}}</h1> <el-button type="primary"
@click="start">开始</el-button> <el-button type="danger"
@click="over">结束</el-button> </div> </template> <script> export default {
data() { return { timer: "", value: 0, }; }, methods: { start(){ this.timer =
setInterval(this.valChange, 2000); // 注意: 第一个参数为方法名的时候不要加括号; }, valChange() {
this.value++; console.log(this.value); }, over(){ clearInterval(this.timer); }
}, mounted() {}, beforeDestroy() { clearInterval(this.timer); }, }; </script>
2. 定时执行 ( setTimeout)
定时执行 setTimeout 是设置一个时间,等待时间到达的时候只执行一次,但是执行完以后定时器还在,只是不再运行;
语法:
setTimeout(code, milliseconds, param1, param2, ...) setTimeout(function,
milliseconds, param1, param2, ...) code/function 必需。要调用一个代码串,也可以是一个函数。
milliseconds 可选。执行或调用 code/function 需要等待的时间,以毫秒计。默认为 0。 param1, param2, ... 可选。
传给执行函数的其他参数(IE9 及其更早版本不支持该参数)。 返回值: 返回一个 ID(数字),可以将这个ID传递给 clearTimeout() 来取消执行。
eg:
开始时候创建一个定时器 setTimeout,只在2秒后执行一次方法。
let timer = setTimeout(() => { //需要定时执行的代码 console.debug("Hello World"); },
2000)