Create a custom callback in JavaScript

前端 未结 10 2184
野性不改
野性不改 2020-11-22 08:08

All I need to do is to execute a callback function when my current function execution ends.

function LoadData() 
{
    alert(\'The data has been loaded\');
          


        
10条回答
  •  醉酒成梦
    2020-11-22 09:00

    When calling the callback function, we could use it like below:

    consumingFunction(callbackFunctionName)

    Example:

    // Callback function only know the action,
    // but don't know what's the data.
    function callbackFunction(unknown) {
      console.log(unknown);
    }
    
    // This is a consuming function.
    function getInfo(thenCallback) {
      // When we define the function we only know the data but not
      // the action. The action will be deferred until excecuting.
      var info = 'I know now';
      if (typeof thenCallback === 'function') {
        thenCallback(info);    
      }
    }
    
    // Start.
    getInfo(callbackFunction); // I know now
    

    This is the Codepend with full example.

提交回复
热议问题