Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:24:10+00:00 2026-06-05T20:24:10+00:00

I am trying to do setInterval inside a class and the code below works

  • 0

I am trying to do setInterval inside a class and the code below works fine in the sense that as the car is created a call to its updatePosition is periodically called.

The problem is that I can not get the value of the @currentSpeed variable in the setInterval “scope”. Instead I get “Updating position: Speed: undefined” in my console.log when the updatePosition function is invoked by the interval.

When I call accelerate() function (which is called anytime I hit the accelerate button) it returns the expected @currentSpeed value

How can I get the value from @currentSpeed in the setInterval scope?

Here is the relevant part of my code:

class Car
    constructor: () ->    
        @currentSpeed = 0

        intervalMs = 1000
        @.setUpdatePositionInterval(intervalMs)

    setUpdatePositionInterval: (intervalMs) ->
        setInterval (do => @updatePosition ), intervalMs

    updatePosition: () ->
        # below logs: "Updating position: Speed: undefined"
        console.log("Updating position: Speed: #{@currentSpeed}")

    accelerate: () ->
        #below logs the expected value of @currentSpeed
        console.log "ACCELERATING! CurrentSpeed: #{@currentSpeed}"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:24:11+00:00Added an answer on June 5, 2026 at 8:24 pm

    There is no point in doing do => @updatePosition to create a callback – because this creates a function (=>) that is immediately executed (due to the do keyword) and returns the function @updatePosition. Thus, you can simplify this to @updatePosition.

    The fat arrow is required at a different location: updatePosition() needs access to the current instance, in order to retrieve the value for @currentSpeed – but since you cannot ensure that this function will always be called in the correct context, you need to bind it to this function by using the fat arrow:

    setUpdatePositionInterval: (intervalMs) ->
        setInterval @updatePosition, intervalMs
    
    updatePosition: () =>
        console.log("Updating position: Speed: #{@currentSpeed}")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call clearInterval inside setInterval function, which is doing some ajax fetching,
I am trying to call a LoadFile() function every X seconds using setInterval, and
I need to use: var obj=document.getElementsByClassName[n]; setInterval(somefunc(+obj+);,10); When I'm trying to run this code,
I am trying to use SetInterval and clearInterval in YUI The code is written
I am trying to call a setTimeout from within a setInterval callback: function callback()
I am trying to build a page that uses jQuery to call a database
I'm trying to detect the existence of a specific cookie abc. The code that
I'm trying to access this.collection from within a setInterval . However, this isn't bound.
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.