How to add a callback to a function in javascript

后端 未结 4 696
既然无缘
既然无缘 2020-12-14 17:52

I have two javascript functions

function one () {
   do something long... like writing jpgfile on disk
}

function two () {
   do something fast... like show         


        
4条回答
  •  执念已碎
    2020-12-14 18:01

    I think it's easy if the browser wait for the process inside "one()" to be done before execute the next line of command. The iceberg hit titanic cause it doesn't wait. Then executing this:

    one(two) // while two is the callBack parameter
    

    is nothing different from:

    one()
    two()
    

    I suggest using a setInterval.

    function one(){
        //--- Write the file to disk
        //.....................
    }
    
    function runTwo(){
        if (check_the_written_file_existence){
             clearInterval(t)
             two();
        }
    }
    var t = setInterval("runTwo()",500)
    

    The most important point is that if there's an event fires when the "long process" in function "one()" has done, you just need to bind function two to that event. Unless, you must check the result by someway every span of time until it's really done.

提交回复
热议问题