JS回调函数的使用和做用

<html>javascript

<head>html

< title >回调函数(callback)</ title >
< script  language="javascript" type="text/javascript">
     function test(){
         var score = document.getElementById("score").value;
         myfun(score,function(){
             alert("这是回调函数,应该在最后");
         });
     }
     function myfun(num,callback){
         if(num< 60 ){
             alert("不及格");
         }else if(num<90){
             alert("良好");
         }else{
             alert("优秀");
         }
         callback();
     }
</script>
</ head >
< body >
< h1 >学习js回调函数</ h1 >
成绩:< input  type="text" id="score"/></ br >
< input  type="button" value="判断" onclick="test()" />
< p >应该能看到调用了两个回调函数</ p >
</ body >
</ html >
 
以上是回调函数的一个示例,来自于:http://www.cnblogs.com/Sunnor/p/4447446.html
它的主要 做用是在当一个函数执行完成后,才来执行此函数。在ajax请求中经常使用于根据不一样的返回结果来处理不一样的任务
相关文章
相关标签/搜索