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

  • Home
  • SEARCH
  • 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 6579335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:53:07+00:00 2026-05-25T15:53:07+00:00

I have an issue with my KnockoutJS code that is creating an infinite loop.

  • 0

I have an issue with my KnockoutJS code that is creating an infinite loop.

Say in my view model I have a value that represents the number of people going on a holiday. This value is bound to a select list, so that a customer can change the number of people.

var viewModel = {
    selectedTravellerCount:  ko.observable(),
    travellers: ko.observableArray([])       
}

When the value of the select list is changed (i.e. the quantity of travellers changes) I want to show an array of select lists to allow the customer to input the age of each traveller.

I assume that I need to create a observable dependent on the selectedTravellerCount that renders in the UI.

viewModel.travellerCount = ko.dependentObservable(function() {
    var tQ = this.selectedTravellerCount();
    alert("count:" + tQ);
        for (var i = 0; i < tQ; i++) {
        alert("adding");
        var t = new traveller();
        alert("a");
        t.Age = 18;
        alert("b");
        this.travellers.push(t);
        alert("added");
    }
}, viewModel);

This works OK when the page is first rendered. However, the first time the select list is changed, the dependentObservable function is repeatedly called and gets as far as the alert("b") line.

So, it looks to me that the traveller is being pushed to the travellers array, which is then causing function to run again, which then gets repeated ad infinitum.

However, I thought the function should only depend on the selectedTravellerCount value changing.

What am I missing?

I’ve also tried:

var viewModel = {
   selectedTravellerCount:  ko.observable(),
   travellers: ko.observableArray([])
   addTraveller: function (age) {
       alert(age);
       this.travellers.push({ age: age });
   }         
}

viewModel.travellerCount = ko.dependentObservable(function() {
    var tQ = this.selectedTravellerCount();
        for (var i = 0; i < tQ; i++) {
            this.addTraveller(18);
    }
}, viewModel);
  • 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-05-25T15:53:08+00:00Added an answer on May 25, 2026 at 3:53 pm

    You need to use a subscription instead of dependsObservable(). A dependsObservable is triggered each time the viewModel changes, hence the name. Adding to the travellers == changing the viewModel

    take a look here

    Subscriptions are described here, at the bottom.

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

Sidebar

Related Questions

I have issue with a value that should have just work, however, it seem
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue with the way we are creating a Mutex . The
I have an issue with watin test that should browse for pdf and upload
i have issue with comparing NAN value in C++, Visualstudio. I need to handle
Much of my sql code is generated (from POD). Now i have issue where
i have issue regarding Paging using filterexpression. here's the piece of code for filtering
i have done drag and drop using with jquery. here i have issue that
I have this issue that I want to resolve. Lets think we have this

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.