[英]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.