简体   繁体   English

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

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

I would like to redirect to the website lilas.com after an animation is played which is in javascript. 播放了JavaScript动画后,我想重定向到lilas.com网站。 I do not understand javascript that much and need help. 我不太了解javascript,需要帮助。

I would like to redirect after this handleComplete function is done and wait for 3 seconds. 完成此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);

It depends on what handleComplete function executes. 这取决于handleComplete函数执行什么。

If this function executes a non async code, you can just call the other function inside a setTimeout like the Helmal and Ameya said in their answers. 如果此函数执行非异步代码,则可以只在setTimeout内调用另一个函数,例如Helmal和Ameya在回答中说的。

If handleComplete executes an asynchronous process, you have to wait until this process finish it's execution, and then wait for 3 seconds to execute the other function. 如果handleComplete执行异步进程,则必须等待该进程完成其执行,然后等待3秒以执行其他功能。

you can wait using settimeout() and call after function execution 您可以等待使用settimeout()并在函数执行后调用

javascript run in single thread so the redirection will take place after execution of your function 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);

It seems your function called "handleComplete" is async function, which means it's not coordinated with the time, to fix that you have 2 ways: 看来您的名为“ handleComplete”的函数是异步函数,这意味着它与时间不协调,可以通过以下两种方法来解决:

1- You need to calculate how much time needed to execute the function and then use setTimeout function. 1-您需要计算执行该功能需要多少时间,然后使用setTimeout函数。 for example if the function needs in average 50 mili seconds to be done you can do the following: 例如,如果该功能平均需要50毫秒来完成,则可以执行以下操作:

window.setTimeout(function(){

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

}, 3050);

2- The second way which is more complex but more efficient is to use the callback idea, you need to change the implementation of handleComplete function and the call to be as following: 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.

相关问题 Javascript-重定向到另一个网站,并在该页面加载后调用Javascript函数 - Javascript - redirect to another website and call Javascript function after that page loads JavaScript:在另一个函数调用完成后,继续执行函数 - JavaScript: Continue with function after another function call finishes 在另一个(异步)函数完成后,在JavaScript中完成一个函数 - Executing a function after an another (asynchronous) function finishes in JavaScript 成功登录后如何使用javascript重定向到另一个页面? - How to redirect to another page using javascript after successful login? 使用JavaScript在Firebase登录后如何重定向到另一个页面? - How to redirect to another page after firebase login using javascript? 10秒后使用javascript和表单提交重定向到另一个页面 - Redirect to another page after 10 seconds using javascript and a form submit 使用 javascript 验证后无法重定向到另一个 html 页面 - Unable to redirect to another html page after validation using javascript 使用javascript函数从asp.net页面重定向到另一个页面 - Redirect from asp.net page to another using javascript function 另一个完成加载后加载新页面 - Load a new page after another finishes loading JavaScript重定向到另一个页面 - Javascript redirect to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM