在另一个函数完成后,使用javascript重定向到另一个页面

时间:2017-01-25 07:28:22

标签: javascript

我想在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);  }

4 个答案:

答案 0 :(得分:2)

您可以使用setTimeout()

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

答案 1 :(得分:2)

这取决于handleComplete函数执行的内容。

如果此函数执行非异步代码,您可以调用setTimeout内的其他函数,如Helmal和Ameya在答案中所说。

如果handleComplete执行异步进程,则必须等到此进程完成执行,然后等待3秒才能执行另一个函数。

答案 2 :(得分:1)

您可以等待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);

答案 3 :(得分:1)

看来你的函数叫做“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);
});