Brayden Rudisill
Brayden Rudisill

Reputation: 13

Atom package - setInterval not calling the function

I'm currently working on a package for atom which involves drawing a curve on a canvas. For whatever reason the following code logs "mouseDown" without ever logging "hi" when handleClickDown is called. I've tried without window. but still "mouseDown" is all that's logged. The handleClickDown function is called every time the canvas is clicked if that context helps at all. I'm sure I'm just not understanding something about how setInterval / coffescript works since I'm new to both atom and coffescript. Any help is appreciated.

@printhi: ->
     console.log "hi"

handleClickDown: (event, element) ->
     console.log "mouseDown"
     @mouseDownId = window.setInterval(@printhi,100)

Edit: The following code seems to run fine

console.log "mouseDown"
@mouseDownId = setInterval(()=>
    console.log "inner"
,75)

however when using a function like this it throws an error that _this.printhi is not a function

@printhi = () ->
    console.log "hi"

handleClickDown: (event, element) ->
    console.log "mouseDown"
    @mouseDownId = setInterval(()=>
        @printhi()
    ,75)

Upvotes: 0

Views: 49

Answers (1)

Brayden Rudisill
Brayden Rudisill

Reputation: 13

Ok just answering my own question here as I figured it out eventually. Turns out I was a bit confused on how @ and => work in coffeescript. You actually need to remove the @ from @printhi: -> so it becomes printhi: -> and then only use it when you're calling it like this @printhi().

Code below worked for me, hope someone finds this helpful

printhi: ->
    console.log "hi"

handleClickDown: (event, element) ->
    console.log "mouseDown"
    @mouseDownId = setInterval(()=>
        @printhi()
    ,75)

Upvotes: 1

Related Questions