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 8965487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:48:51+00:00 2026-06-15T16:48:51+00:00

I need help understanding why the following piece of code returns an undefined object

  • 0

I need help understanding why the following piece of code returns an undefined object property:

var count = 0;
var intervals = {
    collection : []                      
}

intervals.collection[0] = function () {
    this.timer = setInterval(function(){
       count++;
       $("p").html(count);            
    }, 1000);
}();

if(typeof intervals.collection[0] === "undefined") {
    $("span").html("undefined");        
}​

Working Example: http://jsfiddle.net/tvaQk/8/

Basically, I’d like to be able to keep a collection of setIntervals that I can reference later so I can loop through and clear. I was thinking I’d be able to loop through the intervals.collection array and do something like:

clearInterval(intervals.collection[0].timer)

but cannot since intervals.collection[0] is undefined

  • 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-15T16:48:53+00:00Added an answer on June 15, 2026 at 4:48 pm

    You forgot the return.

    intervals.collection[0] = function () {
      return this.timer = setInterval(function(){
    --^^^^^^--
    

    Notice this refers to window, I’m not sure if adding a property timer to the window element is what you were expecting actually. Otherwise, you shall return the interval id to not clutter the global window scope.


    A short way to being able to access the timer using intervals.collection[0].timer is by creating an Object instead:

    intervals.collection.push({
      timer: setInterval(function(){
        count++;
        $("p").html(count);            
      }, 1000)
    });
    
    console.log(intervals.collection[0].timer);
    

    I used Array.push in this example to add the newly created interval as the last element of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help understanding the following Haskell function, split l = rr++[ll] where split
I need help in understanding the following C++ code (in a .h file): bool
I need help in understanding the code snipped below...allocate is a function that would
I'm fixing bugs for some application, and I need help understanding the following lines
I need help understanding whats going on in the function, especially the return statements.
Kind of need help understanding what this code actually outputs. Does it out put
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()
I need some help understanding following shell script line, apphome = `cd \`dirname $0\`
I need some help understanding the Java memory model.The following is a gerneric example

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.