簡體   English   中英

其他功能完成后,使用javascript重定向到另一個頁面

[英]Redirect to another page using javascript after another function finishes

播放了JavaScript動畫后,我想重定向到lilas.com網站。 我不太了解javascript,需要幫助。

完成此handleComplete函數后,我想重定向並等待3秒。

function handleComplete()

function handleComplete() 
{ 
   exportRoot = new lib.Lilas(); 
   stage = new createjs.Stage(canvas); 
   stage.addChild(exportRoot);  
   createjs.Ticker.setFPS(lib.properties.fps);     
   createjs.Ticker.addEventListener("tick", stage); //Code to support hidpi screens and responsive scaling. 
  (function(isResp, respDim, isScale, scaleType) 
   {    
     var lastW, lastH, lastS=1; 
     else if(scaleType==1) 
     {  
       sRatio = Math.min(xRatio, yRatio);   
     }  else if(scaleType==2) 
     {  sRatio = Math.max(xRatio, yRatio);  
     }  
    } 
   })(true,'both',true,2);  }

您可以為此使用setTimeout()

setTimeout(function(){ window.location.href="http://www.lilas.com" }, 3000);

這取決於handleComplete函數執行什么。

如果此函數執行非異步代碼,則可以只在setTimeout內調用另一個函數,例如Helmal和Ameya在回答中說的。

如果handleComplete執行異步進程,則必須等待該進程完成其執行,然后等待3秒以執行其他功能。

您可以等待使用settimeout()並在函數執行后調用

javascript在單線程中運行,因此重定向將在執行函數后進行

function handleComplete();
window.setTimeout(function(){

    // Move to a new location or you can do something else
    window.location.href = "http://www.lilas.com";

}, 3000);

看來您的名為“ handleComplete”的函數是異步函數,這意味着它與時間不協調,可以通過以下兩種方法來解決:

1-您需要計算執行該功能需要多少時間,然后使用setTimeout函數。 例如,如果該功能平均需要50毫秒來完成,則可以執行以下操作:

window.setTimeout(function(){

    // Your redirect is here
    window.location.href = "lilas.com";

}, 3050);

2-更復雜但更有效的第二種方法是使用回調思想,您需要將handleComplete函數的實現和調用更改如下:

function handleComplete( *args*, callback) {

    // the function code, just change return sentence with this line
    return callback();
}

// the call will be like
handleComplete(*args*, function() {
    window.setTimeout(function(){

        // Your redirect is here
        window.location.href = "lilas.com";

    }, 3000);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM