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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:24:08+00:00 2026-06-03T14:24:08+00:00

function parseAttach(b) { var h=; for(i=0;i<b.length;i++) { var a=b[i]; switch(a[‘type’]) { case image: h+='<li

  • 0
function parseAttach(b)
{
    var h="";
    for(i=0;i<b.length;i++)
    {
        var a=b[i];
        switch(a['type'])
        {
            case "image":
                h+='<li class="attach aImg" style="background:#000;border-bottom:2px solid #fff"><img style="max-width:425px;max-height:500px" src="http://img.xiami.com/u/phoo/'+a['file']+'"></li>';
            break;
            case "video":
                h+='<li class="attach aVideo" style="background:#f3f3f3"><span class="MxND" f="'+a['from']+'" d="'+a['id']+'"></span></li>';
            break;
            case "music":
                h+='<li class="attach aMusic"><embed src="http://www.xiami.com/widget/0_'+a['id']+'/singlePlayer.swf" type="application/x-shockwave-flash" width="257" height="33" wmode="transparent"></embed></li>';
            break;
        }
    }
    return h;
}

Once above function is running, the page cannot be interacted, memory and cpu usage of that page skyrocket.

this is an example of parameter b passed to this function:

[{"type":"video","from":"k6","id":"kyxGTZbD-vQ8Domm-eeHiQ"}]

b.length is not more than 2 and this function was executed no more than three times. If this function is removed, memory leaking will not happen.

UPDATE:

Following @GarethMcCaughan ‘s suggestion, I added alert(i) to the top of the loop, it keeps alerting 0, I headed to the invocation code:

for(i=0;i<c[0].length;i++)//the breakpoint
{
    ......
    if(t[6].length>0)
    {
        //console.log(t[6].length);
        //var a=parseAttach(t[6]);
        var a="";
        h+='<ul class="attaches">'+a+'</ul>';
    }
   ......
}

as you see in the comment, if I replace the invocation with a console.log, the log only show 4 times of execution. But why the function are invoked repeatedly?

Then I found the console report a breakpoint at the top of the loop(I’ve comment it out), is this the reason why the function keeps invoking?

  • 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-03T14:24:09+00:00Added an answer on June 3, 2026 at 2:24 pm

    In both of your loops you have not put var before your i variable. This means it is global. If it is global then both loops are using the same i.

    for(i=0;i<b.length;i++)
    

    add var and it should fix it:

    for(var i=0;i<b.length;i++)
    

    EDIT: Further clarification:

    for(i=0;i<c[0].length;i++)
    {
        ......
        if(t[6].length>0)
        {
            // THIS CALL WILL SET THE GLOBAL i TO t[6].length
            var a=parseAttach(t[6]);
        }
       ......
    }
    

    Therefore, the exit condition of the outer loop is never met, every iteration of the loop sets i back so that i never reaches c[0].length.

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

Sidebar

Related Questions

function countChars(elm) { if (elm.nodeType == 3) { // TEXT_NODE return elm.nodeValue.length; } var
function validateRequiredFields(formid) { var inputs = document.getElementsByClassName('required'); for(i=0;i<inputs.length;i++) { var FieldID = inputs[i].id; if(
function deleteRecordDialog() { var returnThis; var numRecordss = recs.length; var html = /*html= html
function randomString( len ) { // A random string of length 'len' made up
function text_window() { var xmlDoc; if (window.XMLHttpRequest) { xmlDoc=new window.XMLHttpRequest(); xmlDoc.open(GET,cd_catalog.xml,false); xmlDoc.send(); xmlDoc=xmlDoc.responseXML; }
function example(){ var quantity = http://www.example.com/index.php?brandid=b%123&quantity=20; quantity = quantity.replace(-what regular expression should i user-,
function dowork() { $(.wrappedElement).removeClass(wrappedElement); $(.wrappedElementout).removeClass(wrappedElementout); var a=$(div#DepPart select option:selected).val(); $(div#TestPart select option[value^=a]).addClass(wrappedElement); $(div#TestPart select
function controllaFoto(target, username) { var _URL = window.URL || window.webkitURL; var img = new
function logsig() { var username = $(#username).val(); var password = $(#password).val(); var dataString =
function averageCalculator (numvalues) { for(i=0, i <= numvalues, i++>) { var score = prompt(input

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.