javascript中命令的顺序

javascript中命令的顺序,javascript,Javascript,在我的演示函数中,我有一个淡出函数。 此函数通过淡出图像来设置图像动画,并运行几秒钟 我希望警报仅在该淡入完成后运行。 . 这样不行。该警报在淡出完成之前弹出 除了计算每个函数的运行时并为每个命令创建相应的setTimeout外,还有什么方法可以对其排序 演示功能: function Demo() { fadeout(liran0); alert("hello"); } function fadeout(element) { var op = 1; // initia

在我的演示函数中,我有一个淡出函数。 此函数通过淡出图像来设置图像动画,并运行几秒钟

我希望警报仅在该淡入完成后运行。 . 这样不行。该警报在淡出完成之前弹出

除了计算每个函数的运行时并为每个命令创建相应的setTimeout外,还有什么方法可以对其排序

演示功能:

function Demo() {
    fadeout(liran0);
    alert("hello");
}
function fadeout(element) {
    var op = 1; // initial opacity
    var timer = setInterval(function () { //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}
分析时需要淡出功能:

function Demo() {
    fadeout(liran0);
    alert("hello");
}
function fadeout(element) {
    var op = 1; // initial opacity
    var timer = setInterval(function () { //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}
功能衰减(元素){
var op=1;//初始不透明度
var timer=setInterval(函数(){//每x毫秒调用一次函数

如果(op如果您查看jQuery库,您会发现它们总是提供一个回调函数,在动画运行完成时调用。例如,查看。它的第二个参数接受一个回调函数,该回调函数在动画运行完成时调用

因此,这里的方法是在
clearInterval()
调用之后调用回调,如下所示

function fadeout(element, complete) {
    var op = 1; // initial opacity
    var timer = setInterval(function () { 
        //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the 
                          // op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            if ( typeof complete === 'function' ) {
                complete();
            }
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}
功能衰减(元件,完成){
var op=1;//初始不透明度
var timer=setInterval(函数(){
//每x毫秒调用一次函数

如果(op您在动画启动后立即调用
警报
,这是异步进行的。请执行以下操作:

function fadeout(element, finished) {
    var op = 1; // initial opacity
    var timer = setInterval(function() { //call the function every x milliseconds
        if (op <= 0.01) { // stop the function when the op is lower then 0.1 and then make in invisible.
            element.style.opacity = 0; // make it invisible
            clearInterval(timer); // clear the timer ....
            if (typeof finished === "function") {
                finished();
            }
            return true;
        }
        element.style.opacity = op;
        op -= 0.01;
    }, 10);
}

fadeout(liran0, function() {
    alert("hello");
});
功能衰减(元件,完成){
var op=1;//初始不透明度
var timer=setInterval(function(){//每x毫秒调用一次函数

if(op)在clearInterval()之后调用fadeout()中的alert,或将回调传递给fadeout(),或使用承诺。使用回调类型checkHa更新!我们写了基本相同的答案。+1表示思想相似的伟大人物。旁白:typeof是一个运算符,但您的代码读起来好像它是一个函数。