简体   繁体   English

jQuery fadeOut回调函数-为什么局部函数将不起作用而全局函数将不起作用?

[英]jQuery fadeOut callback function - why would local function won't work while global function will?

I got a simple twitter bootstrap message frame that I'm write on, with some little jQuery effects. 我得到了一个简单的twitter bootstrap消息框架,上面写着一些jQuery效果。

First I fade out the board, and once it completed fading out, I'm writing what I want to and them fade it in again. 首先,我淡出木板,一旦淡出木板,我就写了我想要的东西,然后他们又将其淡入淡出。

I got 2 versions: 我有2个版本:

the first, using a local defined function : 首先,使用本地定义的函数

function postMessage(message, context) {
    if(typeof context === 'undefined')
        context = "info";

    var newClass = "alert alert-" + context;
    var messageBoard = $("#messageBoard");
    // Fade out
    messageBoard.fadeOut('slow', postFadeOut);

    var postFadeOut = function() {
        alert("XX");
//      $(this).attr('class', newClass);
//      $(this).html(message);
//      // Fade in again
//      $(this).fadeIn('slow');
    }
}

It won't trigger the alert("XX") , yet: 它不会触发alert("XX") ,但是:

function postMessage(message, context) {
    if(typeof context === 'undefined')
        context = "info";

    var newClass = "alert alert-" + context;
    var messageBoard = $("#messageBoard");
    // Fade out
    messageBoard.fadeOut('slow', postFadeOut);
}

function postFadeOut() {
    alert("XX");
//  $(this).attr('class', newClass);
//  $(this).html(message);
//  // Fade in again
//  $(this).fadeIn('slow');
}

Will trigger. 触发。 Why? 为什么?

This is a variable hoisting effect. 这是可变的提升效果。

In JavaScript, variables are hoisted. 在JavaScript中,变量被提升。 That means that in the scope they are declared, they're available in any line of code, even if declared after that line. 这意味着在声明它们的范围内,即使在该行之后声明,它们也可以在任何代码行中使用。 However, their value, or initialization, happens in the order the code is written. 但是,它们的值或初始化按代码编写的顺序进行。 For example: 例如:

 alert(a); var a = 'Some value'; alert(a); 

As you can see, a is available in the first alert (no exception is thrown), but it's uninitialized. 正如你所看到的, a是在第一个警报(没有抛出异常)可用,但它的初始化。

The code above is for all purposes identical to: 上面的代码出于所有目的与以下各项相同:

var a;
alert(a);
a = 'Some value';
alert(a);

In your 1st example, the postFadeOut variable is hoisted like that, yet in the .fadeOut call it's uninitialized and it's value is undefined . 在您的第一个示例中, postFadeOut变量是这样悬挂的,但是在.fadeOut调用中,它未初始化,并且其值是undefined

The 2nd version works since functions are available within the scope they're declared regardless of order of code. 第二个版本可以工作,因为函数在声明的范围内可用 ,而与代码顺序无关。 That's due to the fact that the engine first parses the entire code, "remembering" the functions in that pass, and only then starts executing from the first line forward. 这是因为引擎首先解析整个代码,“记住”该遍中的函数,然后才从第一行开始执行。

Try declaring postFadeOut variable before call to messageBoard.fadeOut('slow', postFadeOut) 尝试在调用messageBoard.fadeOut('slow', postFadeOut)之前声明postFadeOut变量

 function postMessage(message, context) { if(typeof context === 'undefined') { context = "info"; }; var newClass = "alert alert-" + context; var messageBoard = $("#messageBoard"); var postFadeOut = function() { $(this).attr('class', newClass); $(this).html(message); // Fade in again $(this).fadeIn('slow'); } // Fade out messageBoard.fadeOut('slow', postFadeOut); } postMessage("message") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="messageBoard">message board</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM