回调函数jQuery

Kun*_* S. -1 javascript jquery

helloWorld()执行dragTrack()功能后需要运行.但是helloWorld之后没有被召唤dragTrack.

dragTrack(function(){
    helloWorld();
});

function dragTrack() {
    alert('first');
}

function helloWorld() {
    alert('second');
}
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 7

您将函数作为参数传递,但dragTrack需要更改为接受回调并调用它

dragTrack(helloWorld);

function dragTrack(callback) {
  alert('first');
  if (callback) {
    callback();
  }
}

function helloWorld() {
  alert('second');
}
Run Code Online (Sandbox Code Playgroud)