jquery如何将参数传递给回调函数到管道中

时间:2016-02-24 13:51:22

标签: javascript jquery promise jquery-deferred

我有一个简单的例子:

Dim NxtWk As Range, YrFind As Range, MonFind As Range, MonName As Range, _
LngName As String, DerpName As String, DelProd As Range, DelMold As Range, ProdSheet As Sheets, _
MoldSheet As Sheets, WindSheets As Sheets, MonDig As Range, DayDig As Range, FName As String

    Set NxtWk = Sheets("Data").Range("B53")
    Set YrFind = Sheets("Data").Range("C53")
    Set MonFind = Sheets("Data").Range("D53")
    Set MonName = Sheets("Data").Range("E53")
    LngName = MonFind.Value & "-" & MonName.Value & "-" & YrFind.Value
    DerpName = "\\Jupiter\ProductionSchedule\" & "2 Production Schedules"
    'DerpName = "C:\user\dwallace\desktop"
    Set MonDig = Sheets("Data").Range("B59")
    Set DayDig = Sheets("Data").Range("C59")
    FName = MonDig.Value & "-" & DayDig.Value & "-" & YrFind.Value
    YrFold = YrFind.Value

        'Production Ranges

        Set DelProd = Application.Union( _
        Sheets("Production Schedule").Range("H5:AB9"), Sheets("Production Schedule").Range("H15:AB23"), _
        Sheets("Production Schedule").Range("H29:AB30"), Sheets("Production Schedule").Range("H36:AB39"), _
        Sheets("Production Schedule").Range("H45:AB54"), Sheets("Production Schedule").Range("H60:AB62"), _
        Sheets("Production Schedule").Range("H68:AB73"), Sheets("Production Schedule").Range("H79:AB84"), _
        Sheets("Production Schedule").Range("H90:AB94"), Sheets("Production Schedule").Range("H100:AB101"), _
        Sheets("Production Schedule").Range("H107:AB112"), Sheets("Production Schedule").Range("H118:AB119"), _
        Sheets("Production Schedule").Range("H125:AB126"), Sheets("Production Schedule").Range("H132:AB133"), _
        Sheets("Production Schedule").Range("H139:AB140"), Sheets("Production Schedule").Range("H146:AB147"), _
        Sheets("Production Schedule").Range("H153:AB156"), Sheets("Production Schedule").Range("H162:AB166"), _
        Sheets("Production Schedule").Range("H172:AB175"), Sheets("Production Schedule").Range("H181:AB185"), _
        Sheets("Production Schedule").Range("H186:AB186"), Sheets("Production Schedule").Range("H192:AB193"))

        'Molding Ranges

        Set DelMold = Application.Union( _
        Sheets("Molders").Range("B5:W8"), Sheets("Molders").Range("B14:W20"), _
        Sheets("Molders").Range("B26:W31"), Sheets("Molders").Range("B37:W38"), Sheets("Molders").Range("B44:W45"), _
        Sheets("Molders").Range("B51:W54"), Sheets("Molders").Range("B60:W63"), Sheets("Molders").Range("B69:W72"), _
        Sheets("Molders").Range("C78:W93"))

        'Winding Ranges

        Set DelWind = Application.Union( _
        Sheets("Winders").Range("H5:AB6"), Sheets("Winders").Range("H8:AB9"), Sheets("Winders").Range("H11:AB12"), _
        Sheets("Winders").Range("H14:AB15"), Sheets("Winders").Range("H17:AB18"), Sheets("Winders").Range("H20:AB21"), _
        Sheets("Winders").Range("H23:AB24"), Sheets("Winders").Range("H26:AB27"), Sheets("Winders").Range("H29:AB30"), _
        Sheets("Winders").Range("H32:AB33"), Sheets("Winders").Range("H35:AB36"), Sheets("Winders").Range("H38:AB39"), _
        Sheets("Winders").Range("H41:AB42"), Sheets("Winders").Range("H44:AB45"), Sheets("Winders").Range("H47:AB48"), _
        Sheets("Winders").Range("H50:AB51"), Sheets("Winders").Range("H53:AB54"))

'--------------------------------------------------------------------------------------------------
'Booleans to determine what (if any) directories need to be created before a new workbook can be
'created
'--------------------------------------------------------------------------------------------------

    ActiveWorkbook.Save

    'See if a year directory exists.  If it doesn't, create it, then create the month directory, then
    'save the file.
    If Len(Dir(DerpName & "\" & YrFold)) = 0 Then
    MkDir (DerpName & "\" & YrFold)
    MkDir (DerpName & "\" & YrFold & "\" & LngName)
    ActiveWorkbook.SaveAs Filename:=DerpName & "\" & YrFold & "\" & LngName & "\" & FName & ".xlsm"

    'Assuming the Year directory exists, see if the third one (Month) exists. If it doesnt, create it and
    'save the file
    ElseIf Len(Dir(DerpName & "\" & YrFind & "\" & LngName)) = 0 Then
    MkDir DerpName & "\" & YrFold & "\" & LngName
    ActiveWorkbook.SaveAs Filename:=DerpName & "\" & YrFold & "\" & LngName & "\" & FName & ".xlsm"

    'Assuming all necessary directories already exist, save the file
    Else
    ActiveWorkbook.SaveAs Filename:=DerpName & "\" & YrFold & "\" & LngName & "\" & FName & ".xlsm"

    End If

'--------------------------------------------------------------------------------------------------
'Portion of the sub that removes old data from the new workbook
'--------------------------------------------------------------------------------------------------

    DelProd.ClearContents

    DelMold.ClearContents

    DelWind.ClearContents

End Sub

Resulat: param =好的 2 1 我失去了功能之间的同步。 如何通过sync将参数传递给secondFunction进入管道?

1 个答案:

答案 0 :(得分:1)

嗯,你需要做一些小改动:

  

您的代码将立即执行secondFunction并将执行它的返回值作为参数传递给firstFunction,这不太可能是您想要的。

阅读完整的答案:Javascript callback function with parameters

console.log = function(message) {
  $('body').append('<div>' + message + '</div>');
}

function firstFunction(){
  var d = jQuery.Deferred();
  // some very time consuming asynchronous code...
  setTimeout(function() {
    console.log('1');
    d.resolve();
  }, 1000);
  return d.promise();
}
function secondFunction(param){
  console.log('parm = '+param);
  var d = $.Deferred();
  setTimeout(function() {
    console.log('2');
    d.resolve();
  }, 10);
  return d.promise();
}

firstFunction().then(function() {
  secondFunction('OK')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>