带回调和完成的jQuery

时间:2016-08-21 13:21:16

标签: javascript jquery

所以我想在完成动画后在.fadeOut()内使用回调函数。我可以使用以下功能成功完成此操作,没问题。像我想的那样工作(HTML和CSS只是一个黑色的方形div)

function fadeOutThing(speed, callback) {
  $('div').parent().fadeOut(speed, function() {
      if (typeof callback === "function") {
        callback();
      }
  });
}  

function OtherThing() {
    console.log("hello");
}

fadeOutThing(5000, OtherThing);

我真正想要的是,该回调函数有自己的参数,可以是另一个回调函数,如下所示。问题是,当我这样做时,日志将在动画完成之前显示:Here's the fiddle

function fadeOutThing(speed, callback) {
  $('div').parent().fadeOut(speed, function() {
      if (typeof callback === "function") {
        callback();
      }
  });
}  

function OtherThing(stuff) {
    console.log("hello" + stuff); //This displays too soon!
}

fadeOutThing(5000, OtherThing(' stuffsss'));

为什么会这样?我不理解的是什么?

3 个答案:

答案 0 :(得分:1)

问题是因为您在加载页面时立即致电OtherThing()。这意味着您将OtherThing()函数的结果作为回调参数,而不是函数的参考

要执行您需要的操作,您可以为包含OtherThing()电话的回调提供匿名功能:

fadeOutThing(5000, function() {
    OtherThing(' stuffsss'));
});

答案 1 :(得分:0)

绑定参数而不是按如下方式调用函数:

fadeOutThing(5000, OtherThing.bind(this,' stuffsss'));

答案 2 :(得分:0)

你在属性中使用/调用函数,所以代替函数声明你发送它的返回在这种情况下是没有返回所以:

<sec id="sec1">
<para>In addition, many of you will be glad to hear that <xref ref-type="disp-formula" rid="deqn1">(1)</xref> Visual Basic is now a fully object-oriented programming language <xref ref-type="disp-formula" rid="deqn3">(3)</xref>-<xref ref-type="disp-formula" rid="deqn5">(5)</xref>, with the inclusion of the long sought-after class inheritance, as well as other OOP features.</para>
</sec>
<para>In this chapter, you'll see how Visual Basic has evolved eq. <xref ref-type="disp-formula" rid="deqn1">1</xref>  into the VB .NET language of today and get some sense of how and why VB .NET is different from previous versions of Visual Basic.</para>
<sec id="sec1a">
<para>How had I ever managed living without him?
<disp-formula id="deqn1-2">$$\phi=a+b-c^2$$</disp-formula></para>
<para>Gideon Cross. <xref ref-type="figure" rid="fig2">Figure 2</xref>, table 3.</para>
<para>This chapter surveys some of the new features of the .NET Framework <xref ref-type="disp-formula" rid="deqn2">(2)</xref>, <xref ref-type="disp-formula" rid="deqn5">(5)</xref> that most impact the  VB developer. These include namespaces, the Common Language Runtime (CLR), and assemblies.</para>
<para>The third and final section, Part III, consists of the following appendixes:
<disp-formula id="deqn3-5">$$m=a+b-c$$</disp-formula>
</para>
</sec>
</sec>
<sec id="sec2">
<label>2.</label>
<disp-formula id="deqn6">$$\cal {X}=y=1$$</disp-formula>
<para>The switch…case Statement.</para>
<para>A discussion of language changes <xref ref-type="disp-formula" rid="deqn6">(6)</xref> from VB 6 to VB .NET.</para>
<para>A list of VB .NET intrinsic constants, as well as VB .NET enumerations and their members.</para>
</sec>

等于

<sec id="sec1">
<para>In addition, many of you will be glad to hear that <xref ref-type="disp-formula" rid="deqn1-2">(1)</xref> Visual Basic is now a fully object-oriented programming language <xref ref-type="disp-formula" rid="deqn3-5">(3)</xref> and <xref ref-type="disp-formula" rid="deqn3-5">(5)</xref>, with the inclusion of the long sought-after class inheritance, as well as other OOP features.</para>
</sec>
<para>In this chapter, you'll see how Visual Basic has evolved eq. <xref ref-type="disp-formula" rid="deqn1-2">1</xref>  into the VB .NET language of today and get some sense of how and why VB .NET is different from previous versions of Visual Basic.</para>
<sec id="sec1a">
<para>How had I ever managed living without him?
<disp-formula id="deqn1-2">$$\phi=a+b-c^2$$</disp-formula></para>
<para>Gideon Cross. <xref ref-type="figure" rid="fig2">Figure 2</xref>, table 3.</para>
<para>This chapter surveys some of the new features of the .NET Framework <xref ref-type="disp-formula" rid="deqn1-2">(2)</xref>, <xref ref-type="disp-formula" rid="deqn3-5">(5)</xref> that most impact the  VB developer. These include namespaces, the Common Language Runtime (CLR), and assemblies.</para>
<para>The third and final section, Part III, consists of the following appendixes:
<disp-formula id="deqn3-5">$$m=a+b-c$$</disp-formula>
</para>
</sec>
</sec>
<sec id="sec2">
<label>2.</label>
<disp-formula id="deqn6">$$\cal {X}=y=1$$</disp-formula>
<para>The switch…case Statement.</para>
<para>A discussion of language changes <xref ref-type="disp-formula" rid="deqn6">(6)</xref> from VB 6 to VB .NET.</para>
<para>A list of VB .NET intrinsic constants, as well as VB .NET enumerations and their members.</para>
</sec>

要发送函数声明并设置参数,您可以执行第三个参数:

 Dim targetDirectory As String = TextBox1.Text
        Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory, "*.txt")
        For Each txtFile In txtFilesArray
            Dim input As String = File.ReadAllText(txtFile)
            Dim disp As New Regex("<disp-formula id=""deqn(\d+)-(\d+)"">")
            Dim match As Match = disp.Match(input)
            If disp.Matches() Then
                Dim a As Integer = match.Groups(1).Value
                Dim b As Integer = match.Groups(2).Value
                For Each c=a to b in input
                        Dim xref As New Regex("<xref[^>]+rid=""(?<id>deqn\d+-\d+)""[^>]*>(?<content>[^<]+)</xref>")
                    Dim result As String = xref.Replace(input, Function(xyz)
                                                                  ???????
                                                               End Function)
                Next

                File.WriteAllText(txtFile, result)
            End If
        Next

用法:

fadeOutThing(5000, OtherThing(' stuffsss'));

或者第二个选项是使用bind - bind在给定this和给定属性的情况下创建新函数:

fadeOutThing(5000, notDeclaredNothing); //undefined variable

这在全局范围内是窗口对象。