How to activate two JavaScript functions in parallel?

前端 未结 7 854
失恋的感觉
失恋的感觉 2020-12-16 02:23

Anyone can tell me how to activate two (or more) JavaScript AJAX functions in parallel?

7条回答
  •  一个人的身影
    2020-12-16 02:48

    Using several 'setInterval' may make parallel running possible, though it may still run on a single core. The following code is an example of parallelizing a function func with an array of data 'datas'. To run it, use Parallel(func,datas) where func is the name of your global function and datas is an array of data each one as an input for func.

    var i_array=new Array();
    function Parallel(func,datas){
          $(datas).each(function(i,v){
                 i_array[i]=setInterval(function(){
                             clearInterval(i_array[i]);
                             window[func](datas[i]);
                             },10);
                 });
    }
    

    Here is a jsfiddle test. The time stamp in integer numbers show the two ajax were running in parallel.

提交回复
热议问题