Alex
Alex

Reputation: 2076

Cannot read property 'length' of undefined

$ ->
   window.app.helpers = new class Helpers

    constructor: (@name) ->
      @slideRecipeId     = $("#slideRecipe")
      @specialities      = ["A", "B", "C"]

      setTimeout @countUpRecipes, 3000

    countUpRecipes: ->
      @slideRecipeId.html(@specialities[Math.floor(Math.random() * @specialities.length)])

The problem is that on load I get these errors:

Uncaught TypeError: Cannot read property 'length' of undefined 

What's wrong with the code ? Thanks.

Upvotes: 0

Views: 860

Answers (1)

pdoherty926
pdoherty926

Reputation: 10349

You need to bind the context when calling @countUpRecipes:

setTimeout => 
    @countUpRecipes
, 3000

Upvotes: 1

Related Questions