0

I have a function that checks if the URL contains any of navigation hrefs every 100ms and if it does, the certain elements get class active added.

Code:

    var checkActive = function(){
        var path = window.location.pathname,
            path = decodeURI(path),
            path = path.replace(/[\/]/, "");

        if ( window.location.pathname.indexOf(path) > -1 ) {
        $('.navigation .nav li a[href*="' + path + '"]').parent().addClass('active');
        $('.navigation .nav li a:not([href*="'+path+'"])').parent().removeClass('active');
    }

    (function loopingFunction() {
        checkActive();
        setTimeout(loopingFunction, 100);
    })();

Is this checking every 100ms going to affect the performance for other user with weaker PC's ?

P.s: I used this method since I'm using pushState, statechange and .load() to get my content updated, therefore the page itself doesn't refresh.

Dennis Novac
  • 1,003
  • 10
  • 23

1 Answers1

1

It's unlikely to cause any trouble. It's a small check done 10 times a second. But you can make it do less work by just using location.hash and comparing that (unparsed) value with the last one you used:

var lastHash = String(location);
var checkActive = function(){
    if (lastHash === String(location)) {
        return;
    }
    // ...do work...
};

However, you only need this on obsolete browsers. On any vaguely-modern browser, you can use the hashchange event instead, and you can easily detect whether it's supported:

if (typeof window.onhashchange !== "undefined") {
    // Vaguely modern, use the hashchange event
} else {
    // Obsolete, use a workaround
}

(Sorry, you're not just doing hash fragments.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Worked, thanks, but can I know if there is going to be any noticeable difference in a worst case scenario with a 20 years old PC between the two methods? Just curious, since pretty new to jQuery/JS. – Dennis Novac Aug 27 '16 at 11:21
  • @DeneaNovac: The only way I can think of is to get a 20 year-old PC and find out how badly it's impacted. Do you have some reason to believe a significant fraction of your audience will be using PCs designed for the last century? – T.J. Crowder Aug 27 '16 at 11:24
  • The emphasize was on the performance, not audience. Since I'm going to use this method further on, I wanted to know in case the *function in the loop* would be more complex, would there be any performance difference between the two methods. Anyways I didn't make up the question properly and I'll have to check that on my own I guess. – Dennis Novac Aug 27 '16 at 11:34
  • @DeneaNovac: The degree of impact running something roughly 10 times/second will have is obviously **very** dependent on what it does, regardless of the age of the machine running it. I've used timer loops on 100ms and 50ms cycles for things several times and never had any performance issues related to it, but I've always ensured they ran *very* quickly, since I was running them so often. – T.J. Crowder Aug 27 '16 at 11:39