JS callback function

Source: Internet
Author: User

JS callback function (second week)

What is a callback function

In JavaScript, a function can be used as a parameter to another function, which is called a callback function, and the function that takes the callback function as a parameter is called the main function.

callback function Simple notation

Functions that contain callback functions
Function Main (callback) {
....//main function logic.
Callback (A,b,c ...) The main function triggers a certain condition to pass the value to the callback function and executes
}
Call
Main (function callback (A,b,c ...) {
.....//logical handling in callback functions, ...//
})

callback function Scenario Application

Scene: I am 18 years old, ready to find a girlfriend, there are three candidates (girls A, B, C), I confess that the girl will have a period of time to reply to me; I like a girl, but I don't know if a girl also like me, Confession success rate is 0.4, I second like B girl, confession success rate of 0.5, I know girl C very like me, confession into rate of 1, then the question came, who will become my final female ticket?

Scene Simulation: Using MATH.RANDOWM () to generate a random number between [0,1], if this random number small and scale (can indicate the probability of success), then I declare success, with settimeout (), Delay 1000ms, It means the girls need to get back to me for a long time.

Using Java habits to achieve:

functionGetamie(Woman, scale){Woman the girl I confess the probability of success in scale confessionLet temp= Math.Random(); Console.Log(Temp);If(Temp<scale){Console.Log(`${woman}Promise to be my girlfriend '); Temp= Woman;}Else{Console.Log(`${woman}Told me you were a good man.); Temp=False;}SetTimeout(()={Return Temp;},1000);}Call method, calculate who will eventually become my girlfriendIf(!Getamie("Girl A",0.5)){If(!Getamie("Girl B",1) ) {}else {console. Log ( "finally girl B became my girlfriend"  ) }}else{console. Log ( "finally girl a became my girlfriend"  )              /span>           

However, you will find that the result of the implementation is this:

Yes, this is really not what we want, because JavaScript is single-threaded and does not wait until one function finishes executing another function, so writing in this way is not possible, using a callback to implement the mammary gland:

functionGetamie(Woman, scale, callback){Woman the girl I confess the probability of the success of scale confession callback callback function, callback argument for the girl name when the declaration of success, false means to obtain a good witness aLet temp= Math.Random(); Console.Log(Temp);If(Temp<scale){Console.Log(`${woman}Promise to be my girlfriend '); Temp= Woman;}Else{Console.Log(`${woman}Told me you were a good man.); Temp=False;}SetTimeout(()={Callback(Temp);},1000);}Call method, calculate who will eventually become my girlfriendGetamie("Girl A",0.4,(Temp)={If(Temp){Console.Log("Finally, girl a became my girlfriend.");Return;}Else{Getamie("Girl B",0.5,(Temp)={If(Temp){Console.Log("Finally, girl B became my girlfriend.");Return;}Else{Getamie("Girl C",1,(Temp)={if (Temp{console. Log ( "finally girl C became my girlfriend"  ) }else{return;  })  }) }})    /span>                

Look at the effect, refreshed a few times

< finish >
20180803

Play not widely

JS callback function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.