English 中文(简体)
卸载后延迟执行职能
原标题:JavaScript to delay execution of functions after page is loaded
  • 时间:2011-02-25 14:36:38
  •  标签:
  • javascript

我写了这篇大论。 该公司在主页上课以罚款。 我在最后写了警示。

如果我去其他主页,这一警示信息必须发出,但我正在发出警告信息,如果我取消职务,提醒所有页页上的信息,那么我守则中的任何错误都是错误的?

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 );

}
问题回答
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);
}
$(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)";
          });
    });
});

对开端人来说,你没有清除你的定时,你在这里真的是什么? 你们有2种匿名方法,其中1种在半个后触发,1/4后触发。

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 following approach is a summary of the how to use JQuery, if You 我想 use JQuery I ll actual 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 );
    });
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签