window.location不会每次都触发

问题描述 投票:0回答:1

我想知道为什么我写的这个函数只有大约70%的时间都会激发。我希望根据您导航的位置更改一组要点

 function navigatedFrom(currentURL) {
     if(currentURL != oldURL){
              oldURL = currentURL;
              let url = window.location.href.split("=");
              console.log(url[1]);
              if (url[1] == "asset_tracking" ) {
               return (
                   <div>
                     <p className="txt-li">Track assets on maps in real-time</p>
                     <p className="txt-li">
                       Route intelligently with the Navigation SDK
                     </p>
                     <p className="txt-li">
                       Increase revenue by optimizing fleets
                     </p>
                   </div>

                );
              } else if (url[1] == "use-cases_store-locator" ) {
                return (         
                  <div>
                     <p className="txt-li">Help customers find stores, places, and people on maps</p>
                     <p className="txt-li">
                       Take location search on-the-go
                     </p>
                     <p className="txt-li">
                       Add unique functionality to stand out & drive sales
                     </p>
                  </div>

                 );
              } else if (url[1] == "use-cases_turn-by-turn-navigation" ) {
                return (         
                  <div>
                     <p className="txt-li">Fully integrate navigation in your app</p>
                     <p className="txt-li">
                       Route with smarter live traffic
                     </p>
                     <p className="txt-li">
                       Customize the look and feel
                     </p>
                  </div>

                 );
              } else if (url[1] == "use-cases_data-visualization" ) {
                return (         
                  <div>
                     <p className="txt-li">Make informed decisions with geospatial data</p>
                     <p className="txt-li">
                       Build maps presenting data in new ways
                     </p>
                     <p className="txt-li">
                       Create heatmaps, isochrones, choropleths, and 3D maps
                     </p>
                  </div>

                 );
              } else if (url[1] == "on-demand-logistics" ) {
                return (         
                  <div>
                     <p className="txt-li">Build custom in-app navigation for drivers</p>
                     <p className="txt-li">
                       Optimize complex itineraries
                     </p>
                     <p className="txt-li">
                       Improve routing with live traffic
                     </p>
                  </div>

                 );
              }
          }
          oldURL = window.location.href;
          setTimeout(function(){ 
        navigatedFrom(window.location.href);
      }, 1000);

    }
javascript window.location
1个回答
0
投票

使用setTimeout不是一种理想的方法。如果您使用eventListener而不是setTimeout,它可能会像您想要的那样工作。你可以尝试听popstate事件(https://developer.mozilla.org/en-US/docs/Web/Events/popstate),看看它是否有帮助。

window.onpopstate = function(event) {
  console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
© www.soinside.com 2019 - 2024. All rights reserved.