开发者

JavaScript to delay execution of functions after page is loaded

I wrote this javascript to make an animation. It is work开发者_运维技巧ing fine in the home page. I wrote a alert message in the last.

If I go other then home page, this alert message has to come, but I am getting alert message, if I remove the function, alert message working on all pages, any thing wrong in my code?

window.onload = function(){

    var yellows = document.getElementById('magazine-brief').getElementsByTagName('h2');
    var signUp = document.getElementById('signup-link');

        if (yellows != 'undefined' && signUp != undefined){
            function animeYellowBar(num){
                setTimeout(function(){
                    yellows[num].style.left = "0";
                    if(num == yellows.length-1){
                        setTimeout(function(){
                            signUp.style.webkitTransform = "scale(1)";
                        },num*250);
                    }

                }, num * 500);
            }
            for (var i = 0; i < yellows.length; i++){
                animeYellowBar(i);
            }
        }

    alert('hi');

}


  • DEMO: http://jsbin.com/enaqu5/2
var yellows,signUp;
window.onload = function() {
    yellows = document.getElementById('magazine-brief').getElementsByTagName('h2');
    signUp = document.getElementById('signup-link');
    if (yellows !== undefined && signUp !== undefined) {
        for (var i = 0; i < yellows.length; i++) {
            animeYellowBar(i);
        }
    }
  alert('hi')
}
function animeYellowBar(num) {
    setTimeout(function() {
        yellows[num].style.left = "0";
        if (num == yellows.length - 1) {
            setTimeout(function() {
                signUp.style.webkitTransform = "scale(1)";
            },
            num * 250);
        }
    },
    num * 500);
}
  • DEMO 2: http://jsbin.com/utixi4 (just for sake)
$(function() {
    $("#magazine-brief h2").each(function(i,item) {
          $(this).delay(i+'00').animate({'marginLeft': 0 }, 500 ,function(){
           if ( i === ( $('#magazine-brief h2').length - 1 ) )
             $('#signup-link')[0].style.webkitTransform = "rotate(-2deg)";
          });
    });
});


For starters you are not clearing your SetTimeout and what are you truly after here? You have 2 anonymous methods that one triggers after half a second and the other triggers a quarter of a second later.

So this is just 2 delayed function calls with horribly broken syntax.

Edited Two possibilities, one fixes your current code... the latter shows you how to do it using JQuery which I would recomend:

var yellows, signUp;

window.onload = function(){
    yellows = document.getElementById('magazine-brief');
    if(yellows != null){
        yellows = yellows.getElementsByTagName('h2');
    }else{
        yellows = null;
    }
    signUp = document.getElementById('signup-link');

    if (yellows != null && signUp != null && yellows.length > 0)
    {
        for(var i = 0; i < yellows.length; i++)
        {
            animeYellowBar(i);
        }
    }
    alert('hi');
}

function animeYellowBar(num)
{
    setTimeout(function(){
        yellows[num].style.left = "0";
        if(num == yellows.length-1){
            setTimeout(function(){
                signUp.style.webkitTransform = "scale(1)";
            },num*250);
        }
    }, num * 500);
}

The below approach is a SUMMARY of how to use JQuery, if you want to use JQuery I'll actually test it out:

//Or using JQuery
//Onload equivelent
$(function(){
    var     iterCount = 0,
        maxIter = $("#magazine-brief").filter("h2").length;
    $("#magazine-brief").filter("h2").each(function(){
        setTimeout(function(){
            $(this).css({left: 0});
            if(iterCount == (maxIter-1))
            {
                setTimeout(function(){  
                    signUp.style.webkitTransform = "scale(1)";
                    },iterCount*250);
            }
        }, iterCount++ * num );
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜