Home > database >  How to find which js file creating problem
How to find which js file creating problem

Time:12-08

I have more than 20 js files like jQuery etc in my project, when I scroll it gets hung as it loads very lazily. How to find which file creates the problem?

CodePudding user response:

It may be one of several things and without looking at your code I couldn't possibly say what the cause would actually be. You've asked an extremely subjective comment. There's no silver bullet when it comes to debugging and problem solving.


I've always found the Occams Razor approach the most effective.

"When you remove all other options, whatever remains, however unlikely, must be the problem/solution/answer."


Firstly, before you start removing JS files, have you tried doing a full-search of your project for .scroll? There's a high likelihood that there are several functions which run on-scroll which are causing reflows, a common problem which such code.

Once you've assessed your code, you can verify exactly what happens when the code executes using the "Performance" tab in Google Chrome to do this (Learn how to use the Performance Tab here). You can take the appropriate action.


Assuming that your code suffers from the same problem I've encountered in my formative years using jQuery - multiple or problematic on-scroll events - you can de-bounce the ones which can run after scrolling has completed. You can do this by using the following pattern.

Scenario 1: This would run many times. 'N' times for each scrollwheel drag (dependent on settings - mine is 10) and even more times when using the scrollbar.

function someFunc() {
    let someArr = [];

    for(var i = 0; i < 1000000; i   {
        someArr.push((i * 2));
    }
    
    for(var i = 0; i < someArr.length; i   {
        someArr[i] /= 0.25;
    }
}


$(window).on("scroll", function() {
    someFunc();
});

Scenario 2:

This would run once after scrolling has finished. Waiting for 200ms before executing to ensure the user has completely finishing scrolling.

function someFunc() {
    let someArr = [];

    for(var i = 0; i < 1000000; i   {
        someArr.push((i * 2));
    }
    
    for(var i = 0; i < someArr.length; i   {
        someArr[i] /= 0.25;
    }
}

let debouncedTimeout = null;

$(window).on("scroll", function() {
    if (debouncedTimeout) {
        clearTimeout(debouncedTimeout);
    }

    debouncedTimeout = setTimeout(someFunc(), 200);
});

CodePudding user response:

Add a console.log("Check") with numbers (check1, check2) and so on in every file. Check your console in your browser, look in which series they load in console. If it takes a while to load the next log you know its the previous file with the number you logged.

CodePudding user response:

Your console should say everything. But loading so many js files is bad practice. Try to fit everything in to one, so in further bckend development can go to an .min.js But if you keep doing this. The simplest way is to keep track of the funcioncesequences with console.log, so that you evrything works how it is supposed to :)

  • Related