Chrome:限制历史记录状态更改,以防止浏览器挂起。 ReactJs和jQuery

时间:2018-07-18 11:36:57

标签: jquery reactjs google-chrome

  

当页面转到特定高度时,我只是在更改URL。但   它显示了我的错误   chrome:限制历史记录状态更改为   阻止浏览器挂起。。但它正在工作   在Firefox中很好,所以有人知道如何防止这种警告   如果是,请告诉我?

 function myFunction(){
            var h = ($("html").scrollTop());
                        if(h == 0){
                             window.history.pushState(null,null,'/');
                            // console.log("0oo");
                        }
                        else if(h >= 1 && h <= 600){
                             window.history.pushState(null,null,"abc");
                            // console.log("test");
                        }
                        else if (h >= 600 && h <= 1200) {
                            history.pushState(null, null, 'def');
                            // console.log("test1");
                        }
                        else if (h >= 1200 && h <= 1800) {
                            history.pushState(null, null, 'ghi');
                            // console.log("test2");
                        }
                        else if (h >= 1800 && h <= 2400) {
                            history.pushState(null, null, 'jkl');
                            // console.log("test3");
                        }
                        else if (h >= 2400 && h <= 3000) {
                            history.pushState(null, null, 'mno');
                            // console.log("test4");
                        }
                        else if (h >= 3000 && h <= 3600) {
                            history.pushState(null, null, 'pqr');
                            // console.log("test5");
                        }
                        else if (h >= 3600 && h <= 4200) {
                            history.pushState(null, null, 'stu');
                            // console.log("test6");
                        }
                        else{

                        }
    }

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题-我通过检查当前路径名来解决它,以查看在更改前是否需要进行更改。这大大减少了历史状态更改的次数,因此避免了此警告。

将此想法应用于您的代码,我会这样做:

if(h == 0){
    if (window.location.pathname !== "/") {
        window.history.pushState(null,null,'/');
    }
}
else if(h >= 1 && h <= 600){
    if (window.location.pathname !== "abc") {
        window.history.pushState(null,null,"abc");
    }
}
... etc
else if (h >= 3600 && h <= 4200) {
    if (window.location.pathname !== "stu") {
        window.history.pushState(null,null,"stu");
    }
}

相关问题