window.location每次都不会触发

时间:2018-03-05 22:17:12

标签: javascript window.location

我想知道为什么我写这个函数只有大约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);

    }

1 个答案:

答案 0 :(得分: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));
};