Skip to content

[js] 第10天 简要描述下什么是回调函数并写一个例子出来 #30

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第10天 简要描述下什么是回调函数并写一个例子出来

Activity

tiyunchen

tiyunchen commented on Jun 1, 2019

@tiyunchen

通常将一个函数B传入另一个函数A,并且在 需要的时候再调用函数A。
promise 就有回调

myprelude

myprelude commented on Jun 13, 2019

@myprelude
dom.addEventerlisten('click',function(){
  // do something
})
Damon99999

Damon99999 commented on Jun 18, 2019

@Damon99999
(callback) => {
    return (...args) => {
        const self = this;
        setTimeout(() => {
            callback.apply(self, args);
        }, 200);
    };
};
AricZhu

AricZhu commented on Jun 19, 2019

@AricZhu

回调函数就是指函数在初始定义的时候先不执行,等满足一定条件以后再拿出来执行。如下:
setTimeout(() => { console.log('在本轮任务最后执行!') }, 0);

persist-xyz

persist-xyz commented on Jun 29, 2019

@persist-xyz
asyncFn: () => {
        // 1
        $('body').onclick = e => {
            console.log(e)
        }

        // 2
        document.querySelector('body').addEventListener('click', e => {
            console.log(e)
        })

        // 3
        setTimeout((e) => {
            console.log(e)
        }, 100)

        // 4
        $ajax('/url', res => {})
    }
Konata9

Konata9 commented on Jul 6, 2019

@Konata9

回调函数首先作为一个函数的参数传入,当这个函数执行后再执行的函数,往往会依赖前一个函数执行的结果。
javascript 中,对于 I/O、HTTP 请求等异步操作,为了控制执行的顺序就需要使用回调的方法。

// 第三个参数就是回调函数
function func1(param1, param2, ..., callback){
  // To do some action
  // 往往会在最后调用 callback 并且传入操作过的参数
  callback(cbParam1, cbParam2, ...)
}

// 实际调用的时候
func1(param1, param2, ..., (cbParam1, cbParam2, ...) => {
  // To do some action
})

当有过个任务需要顺序执行时,如果采用回调函数的形式就会出现我们熟悉的“回调地狱”的情况。为了解决这个问题,在 ES6 中就有了 Promiseasync/await 方法。
目前看来 async/await 在异步写法上较为优雅。

15190408121

15190408121 commented on Jul 13, 2019

@15190408121

简单的这个算吗
var i = 0;
function callBack(I) {
if (I < 10) {
console.log(i)
callBack(i++)
} else {
console.log("回调成功")
}
}
callBack(i)

Vi-jay

Vi-jay commented on Jul 25, 2019

@Vi-jay

简单的这个算吗
var i = 0;
function callBack(I) {
if (I < 10) {
console.log(i)
callBack(i++)
} else {
console.log("回调成功")
}
}
callBack(i)

不算 你这是递归

Vi-jay

Vi-jay commented on Jul 25, 2019

@Vi-jay

回调是把一个函数作为参数传递给另一个函数,当该函数满足某个条件时触发该参数函数。
主要用于异步操作 例如网络请求 防止页面同步代码阻塞导致渲染线程停止

function longTask(callback,timeout) {
  setTimeout(callback,timeout)
}
longTask(()=>{console.log("回调任务被执行了");},2000);
console.log("我是同步代码 不会阻塞我");
hc951221

hc951221 commented on Aug 7, 2019

@hc951221

function a() {
console.log('触发了函数a')
}
function b() {
console.log('触发了函数b')
}
function c(callback1, callback2) {
let num = Math.random() * 10
if (num > 6) {
callback1()
} else {
callback2()
}
}
c(a,b)

15190408121

15190408121 commented on Aug 25, 2019

@15190408121

// 比较简单的就是快排算法
function quick(arr) {
if(arr.length <= 1) {
return arr; //递归出口
}
var left = [],
right = [],
current = arr.splice(0,1); //注意splice后,数组长度少了一个
for(let i = 0; i < arr.length; i++) {
if(arr[i] < current) {
left.push(arr[i]) //放在左边
} else {
right.push(arr[i]) //放在右边
}
}
return quick(left).concat(current,quick(right)); //递归
}

xcLtw

xcLtw commented on Sep 2, 2019

@xcLtw

[1,2,3].map(x=>x+1)
按照依赖前置函数,作为参数传入的条件,这个是回调函数吧

Yulingsong

Yulingsong commented on Sep 25, 2019

@Yulingsong

不知道这个算不算,以前对请求方法做了一个简化,success和error算是回调函数吧,我的理解就是把函数当做参数放到另一个函数中。。

 GET: function (url, data, success, error) {
    $.ajax({
      type: 'get',
      url: url,
      dataType: "JSON",
      success: function (result) {
        success(result)
      },
      error: function erryFunction(err) {
        error(err);
      }
    });
  },

GET(url,data,function(res){
    //deal success result
},function(err){
    //deal error result
})

25 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@Konata9@laboonly@Yulingsong

        Issue actions

          [js] 第10天 简要描述下什么是回调函数并写一个例子出来 · Issue #30 · haizlin/fe-interview