Friday, 27 April 2018

javascript callback

start task


A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

<div class="show_visitor" onclick="task1(task2)">start task</div>

<script>

function task4(pre_task){
  alert('previous task: '+ pre_task+' current task 4');
}

function task3(pre_task, callback){
  alert('previous task: '+ pre_task+' current task 3');
  callback('task3');
}

function task2(pre_task,callback) {
  alert('previous task: '+ pre_task+' current task: task 2');
  callback('task 2',task4);
}

function task1(callback) {
  alert('current task 1');
  callback('task 1',task3);
}

reference:
https://codeburst.io/javascript-what-the-heck-is-a-callback-aba4da2deced
https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

No comments:

Post a Comment