setInterval 实现数组数据循环

 代码如下
let index = 0   this.y = setInterval(() => { const data = [1, 2, 3, 4] const len = data.length if ((index + 1) >= len) { console.log(data[index]) index = 0 } else { console.log(data[index]) index++ } }, 2000)

    

另外,setTimeOut定时器实现数组内容循环获取也可以实现 

直接调佣clearInterval 清除无效

window.clearInterval方式

  destroyed () {
    window.clearInterval(this.y)
  },

✧(≖ ◡ ≖✿  ✧(≖ ◡ ≖✿ 

#####彩蛋

clearInterval === window.clearInterval
// true
思考:为何clearInterval  清除无效
可能性答案: JavaScript命名空间、闭包等原因导致功能无效 o(╥﹏╥)o
欢迎大脑留言
posted @ 2022-04-26 14:49  树叶铃铛  阅读(257)  评论(0编辑  收藏  举报