Js倒计时实现

两种思路

  • 使用setTimeout,每次延迟执行1S后,再次调用自身。
  • 使用setInterval,1S执行一次,最后判断计时器是否为0。
			startCountDown: function() {
				var that = this
				that.countDown = 1500
				that.countSecondWithSTO()
			},
			//用setTimeOut
			countSecondWithSTO: function() {
				var that = this
				var timeOutId
				log(that.countDown)
				if (that.countDown === 0) {
					Notification.requestPermission(function(status){
						var n = new Notification('时间到', {body: '休息一下'})
					})
				} else {
					that.countDown--
					timeOutId = setTimeout(this.countSecondWithSTO, 1000)
				}
			},
			//用setInterval
			countSecondWithSTI: function() {
				var that = this
				log(that.countDown)
				if (that.countDown === 0) {
					Notification.requestPermission(function(status){
						var n = new Notification('时间到', {body: '休息一下'})
					})
				} else {
					that.countDown--
				}
			}

你可能感兴趣的:(大杂烩,javascript)