Javascript Question

Hi,

 I have attached the  script where I iterate through a list on one site and fetch values and then get list items from another list on another site and the title field of the first list should match the description of the URL field in the second list. and a table is built using the combined values. How do I modify this script so it is much more efficient? Right now it is a bit slower. Please help., also am doing a string comparison in the below script which is sometimes failing even though the strings match. What Am I doing wrong?
script.txt
TechsavyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Hi mate...
Like this out of nothing, with no concrete example it's hard to figure out what's going on.

On the topics you have SharePoint...
Is this IFrames running Provider Hosted Apps that you want to extract data from?
0
Alexandre SimõesManager / Technology SpecialistCommented:
Reading your code I can already see one improvement that will boost the process.

On the getprojects function you add a row the the table on each loop.
Don't do it like this. Instead, only change the DOM once at the end, which is the same to say that only do one $('#statusTable').append(...) at the end of the loop.

Specially if you have a lot of rows you'll see a big difference.
Here's the modified code:
$(document).ready(function () {
    getprojects();
});

function getprojects() {

    var hostweb = getSiteUrl();
    var url = hostweb + "/_api/web/lists/getbytitle('Related Projects')/items?";
	var htmlRows = [];
	
    $.ajax({
        url: url,
        method: "GET",
        async: false,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {

            if (data.d.results.length > 0) {

                for (var i = 0; i < data.d.results.length; i++) {
                    var projectName = data.d.results[i].Title;
                    var projectURL = data.d.results[i].ProjectURL;
                    htmlRows.push(getProjectRow(projectName, projectURL));
                }

            }
            else {
                htmlRows.push('<tr class="mrvl-tblRow"><td colspan="2">No Related Projects Added</td></tr>');
            }
			
			$('#statusTable').append(htmlRows.join(''));
        },
        error: function (data) {
            //failure(data);
        }
    });
}

function getProjectRow(projectTitle, projectLink) {
    var projectName;
    var projectURL;
    var projectStatus;

    var listItemInfo = '';
    //Make a REST CALL to read all Items from the list.
    var pmsiteUrl = "http://mysite.com/it/pm/_api/web/lists/getbytitle('Dashboard')/items?$select=Project,Project_x0020_Status";


    $.ajax({
        url: pmsiteUrl,
        method: "GET",
        async: false,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {

            if (data.d.results.length > 0) {
                for (var i = 0; i < data.d.results.length; i++) {
                    if (data.d.results[i].Project != null) {
                        projectName = data.d.results[i].Project.Description;
                    }
                    else {
                        projectName = "";
                    }

                    if (data.d.results[i].Project != null) {
                        projectURL = data.d.results[i].Project.Url;
                    } else {
                        projectURL = "";
                    }

                    if (data.d.results[i].Project_x0020_Status != null) {
                        var label = data.d.results[i].Project_x0020_Status.Label;
                        projectStatus = getStatusIndicator(label);
                    } else {
                        projectStatus = "-";
                    }

                    if (projectName == projectTitle) {
                        listItemInfo += "<tr class='mrvl-tblRow'><td>" + "<a href='" + projectURL + "'target='_blank'>" + projectName + "</a></td><td style='text-align:center;'>" + projectStatus + "</td></tr>";
                    }
                }

                return listItemInfo;
            }
            else {
                alert('Could not Contact IT PM Site');
            }
        },
        error: function (data) {
        }
    });

}



//getstatusindicator

function getStatusIndicator(statuslabel) {
    var returnString = "";

    switch (statuslabel) {
        case "60":
            returnString += "<img src='_layouts/images/kpinormal-2.gif' title='Red' alt='Red'/>";
            break;
        case "57":
            returnString += "<img src='_layouts/images/kpinormal-1.gif' title='Yellow' alt='Yellow'/>";
            break;
        case "55":
            returnString += "<img src='_layouts/images/kpinormal-0.gif' title='Green' alt='Green'/>";
            break;
        default:
            returnString += "-";
    }


    return returnString;

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve BinkCommented:
Here's a couple articles I came across recently.  The second link has a really good demonstration of the savings involved with each optimization.

https://developers.google.com/speed/articles/optimizing-javascript
http://www.webreference.com/programming/javascript/jkm3/index.html
0
TechsavyAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.