Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

JS: How to prevent race condition?

General Tech Bugs & Fixes

Max. 2000 characters
Replies

usr_profile.png

User

( 6 months ago )

My task is to improve the performance on an existing WordPress site therefore I installed cache and a race condition started happening.

This is the site. The full code is here, it doesn't fit in the editor, on line 474 there's this function

function loaded() {
    var e = $("#content-wrap").attr("data-classes");
    if ($("body").attr("class", e), $("#news-slider").length) {
        var t = $("#news-slider").attr("data-timer");
        $("#news-slider").slick({
            dots: !1,
            autoplay: !0,
            speed: 800,
            autoplaySpeed: t,
            fade: !0,
            cssEase: "linear",
            prevArrow: "<span class='prev-slide'><i class='fa fa-angle-left'></i></span>",
            nextArrow: "<span class='next-slide'><i class='fa fa-angle-right'></i></span>"
        })
    }

...

this line

$("#news-slider").slick({

gives slick not defined, sometimes, again it's a race condition. Sometimes an error in another function below it, the problem starts from loaded() function and below. Now I stopped caching the file and the homepage, where the file is loaded, the problem persists.

If I use $(window).load(function() either on the entire file or just this function, the homepage would be messed up, no errors would be visible in console though.

If I use $(document).ready(function() either on the entire file or just this function, the slider on mobile would be messed up, no errors would be visible in console though.

If I set a timer to delay the execution of the script, it wouldn't solve anything.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif