正在停止,对象中的计时器功能

时间:2018-12-12 07:59:33

标签: javascript arrays node.js

从我尝试使用的控制台中清除setInterval

clearInterval(obj.timer())

这个想法似乎并没有解决问题

var obj = {
  timer: function timer() {
       setInterval( () => { console.log('welcome') },1000)
  }
}

obj.timer()

// how do I clearInteval from the console

2 个答案:

答案 0 :(得分:1)

您需要存储class FrontShadowLayer: CALayer { private let shadowLayer: CALayer override var bounds: CGRect { didSet { updateShadowPath() } } override var frame: CGRect { didSet { updateShadowPath() } } override init() { shadowLayer = CALayer() super.init() shadowSetup() } required init?(coder aDecoder: NSCoder) { shadowLayer = CALayer() super.init(coder: aDecoder) shadowSetup() } private func shadowSetup() { self.addSublayer(shadowLayer) shadowLayer.frame = bounds shadowLayer.shadowColor = UIColor.gray.cgColor shadowLayer.shadowOpacity = 0.8 updateShadowPath() } private func updateShadowPath() { shadowLayer.shadowPath = CGPath(rect: bounds, transform: nil) } } 函数返回的ID。

setInterval

您可以通过调用停止功能 var obj = { id: null, timer: function timer() { this.id = setInterval(() => { console.log('welcome'); }, 1000) }, stop: function stop() { clearInterval(this.id); } } obj.timer();

来停止计时器

答案 1 :(得分:0)

您尝试过此操作,在功能中添加“ return”

var obj = {
            timer: function timer() {
                 return setInterval( () => { console.log('welcome') },1000)
            }
           };
var timer = obj.timer();
//clear timer
clearInterval(timer);