English 中文(简体)
我的javascript for statement造成了什么fin?
原标题:What s causing the infinite loop in my javascript for statement?
  • 时间:2010-11-17 16:14:01
  •  标签:
  • javascript

在进入庇护所时,它从未停止:

 remove: function remove(e) {
     var objectToRemoveId = e.currentTarget.getAttribute( objectId ).toString();
     var filteredList = this.myDto.objectList;

     for (var index = 0; index < this.myDto.objectList.length; index++) {
          var currentObject = this.myDto.objectList[index];

       if (currentObject.Id !== objectToRemoveId) {
         filteredList[filteredList.length + 1] = timeSheet;
       }
     }
  } 

假设这支运动是一个阵列,其中有一个要素。 我确信,问题在我面前只是悲观,但我可以说出。

最佳回答

You re adding to the same list in your loop, so every time you loop through, your this.myDto.objectList.length goes up one. It seems like you would want an empty array here:

var filteredList = this.myDto.objectList;

与此类似:

var filteredList = [];

或副本,如:

var filteredList = this.myDto.objectList.slice();

我不敢肯定最终结果是什么上下了<>>>>,在一份被过滤的名单中添加了字眼,但不管怎么说,你都很可能是在上述解决办法之一之后。

问题回答

这是因为您正在修改名单,即:将名单提升到每张,从而提升到每张名册上。

混淆很可能是因为提及:

var filteredList = this.myDto.objectList;

然而,你仍在使用“<条码>这一条码”。

你们能够做些什么来减少混乱?

 remove: function remove(e) {
     var objectToRemoveId = e.currentTarget.getAttribute( objectId ).toString();
     var filteredList = new Array();

     for (var index = 0; index < this.myDto.objectList.length; index++) {
          var currentObject = this.myDto.objectList[index];

       if (currentObject.Id !== objectToRemoveId) {
         filteredList[index] = timeSheet;
       }
     }
   } 

有两个错误,一起造成无限循环:

  1. filteredList is a reference to this.myDto.objectList - both factors refer to the same Object. 当你估计会增加过滤器的长度时,你也增加了我的Dto.objectList的长度。 问题的这一部分解决办法是使用“slice()”方法复制阵列。

  2. 此外,你还利用严格的比较操作员(!=)寻找你的物体。 移除 同上。 由于该补贴可能作为数字价值储存,而你则称其属性为:never<>em>,因此,如果你每次通过该休息室发言,这些数值可以平等。

你们把这两个方面加在一起,而且随着你们的阵列,你们最终将每一条热点增加一个任期。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.