end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

javascript:setTimeout() で sleep()代替

setTimeout()以外のことも書いていますが、以下の通りです。

(function() {
    var directionsService = new google.maps.DirectionsService();
    var SLEEP_CALC_DIST = 500; //msec. google map apiでroute計算する実行間隔
    
    var CalcDist = function() {};
    CalcDist.prototype = {
        
        startCalcDists: function(){
            $('#calcResult').val(''); //結果欄のclear
            
            //スタート地点の座標
            this.start_lon_lat =
                [ $('#start_lon').val(), $('#start_lat').val() ];
            //ゴール地点の座標
            this.goals_str =  $('#goal_lon_lat').val().split("\n");
            
            this.calcDists(0);
        },

        calcDists: function(i){
            var goal_lon_lat = this.goals_str[i].split("\t");
            if(goal_lon_lat.length < 2){
                return false;
            }

            this.calcDist(new google.maps.LatLng( this.start_lon_lat[0],
                                                  this.start_lon_lat[1]),
                          new google.maps.LatLng( goal_lon_lat[0],
                                                  goal_lon_lat[1]) );

            var this_obj = this;
            setTimeout( function(param_i) {  //////// HERE!!
                this_obj.calcDists(param_i); //////// HERE!!
            }, SLEEP_CALC_DIST, i+1);        //////// HERE!!

        },

        calcDist: function(start_lon_lat, goal_lon_lat){
            
            //google map apiによる経路探索
            directionsService.route(
                {origin: start_lon_lat,
                 destination: goal_lon_lat,
                 travelMode: google.maps.DirectionsTravelMode.DRIVING,
                 unitSystem:
                 google.maps.DirectionsUnitSystem.METRIC, //m表示
                 optimizeWaypoints: true,//最適化された最短距離にする
                 avoidHighways: true,    //true=高速道路を使用しない
                 avoidTolls: true        //true=有料道路を使用しない
                },
                function(result, status) {
                    //google map apiからOKが返ってきたら、結果欄に表示
                    if (status == google.maps.DirectionsStatus.OK) {
                        //google map apiは複数のルートを返しますが
                        //「optimizeWaypoints: true」により
                        //最初が最短経路のはず...
                        var distance =
                            result.routes[0].legs[0].distance.value;
                        var end_location =
                            String( result.routes[0].legs[0].end_location );
                        
                        var match_result = end_location.match(/([\d\.]+)/g);
                        
                        var caltResultStr =
                            [match_result[0],
                             match_result[1],
                             distance].join("\t");
                        $('#calcResult').val(
                            $('#calcResult').val()+ caltResultStr+"\n");
                        
                    }
                });
        }
    };

    window.calcDist = new CalcDist();
})();