2011年12月29日 星期四

處理呼叫API 的 FOR loop~

有些 Web API (google map or ...) 不能讓你直用 for 呼叫數次,因此,要採用 setTimeout 處理, 如下...


<html>
<head>
    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //準備從1/1做到5/31
            var d = new Date(2011, 0, 1);
            var june = new Date(2011, 5, 1);
            //將待處理的日期放進Array中
            var jobQueue = [];
            while (d < june) {
                var yy = d.getFullYear();
                var mm = d.getMonth() + 1;
                if (mm < 10) mm = "0" + mm;
                var dd = d.getDate();
                if (dd < 10) dd = "0" + dd;
                //產生yyyy/MM/dd格式日期
                   jobQueue.push(yy + "/" + mm + "/" + dd);
                d.setDate(d.getDate() + 1);
            }

            var $body = $("body");
            function run() {
                //檢查是否還有待處理工具
                if (jobQueue.length > 0) {
                    s = jobQueue.shift();
                    $.post("DoSomething.aspx?date=" + s, {}, function (r) {
                        //顯示執行結果
     $body.append("<div>" + s + ":" + r + "</div>");
                        //使用setTimeout可調節連續執行的速度
      setTimeout(function () {
                            run();
                        }, 10);
                    });
                }
            }
            run();
        });
    </script>
</head>
<body></body></html>

這樣就可以處理回圈呼叫了~

Ref: http://blog.darkthread.net/post-2011-06-01-ajax-loop-with-js.aspx

當..Dom還沒建立卻要塞東西時...

要檢查狀態,要這樣....

....

//利用setInterval 0.1秒觸發一次檢查
var hnd = setInterval(function () {
var t = $(frm).contents().find("div").text();
//若已取到資料
if (t) {
//顯示結果
showMsg("Ready + setInterval Trick for " + frm.id + " -> " + t);
//終止setInterval輪詢檢查
clearInterval(hnd);
}
}, 100);
....


原始Codes

var $frms = $("#frmA,#frmB");
$frms.load(function () {
showMsg("Load Event for " + this.id + "->" +
$(this).contents().find("div").text());
});
setTimeout(function () {
$frms.each(function () {
//在ready事件中,this不會指向IFrame,故這裡另設變數保存之
var frm = this;
$(frm).ready(function () {
//ready事件不保證DOM已載入完成,故可能取不到資料
showMsg("Ready Event for " + frm.id + " -> " +
$(frm).contents().find("div").text());
//利用setInterval 0.1秒觸發一次檢查
var hnd = setInterval(function () {
var t = $(frm).contents().find("div").text();
//若已取到資料
if (t) {
//顯示結果
showMsg("Ready + setInterval Trick for " +
frm.id + " -> " + t);
//終止setInterval輪詢檢查
clearInterval(hnd);
}
}, 100);
});
});
}, 1000);


Ref. http://blog.darkthread.net/post-2011-10-27-iframe-load.aspx

2011年12月27日 星期二

老師

求學的過程中...,一路上,我都一直遇到好老師,或許應該說,想當老師的長輩們都是好好人.不過,最近卻開始發現,當人被教育,並開始產生自己的主見後,身邊的老師就慢慢的變少了.這種狀況不是我要的,畢竟,身邊所發生不好的事,應該要轉為提醒自己的借鏡,好的事,理當是自己要學的.在此紀錄並提醒自己!

2011年12月5日 星期一

Google Map V3 MarkerCluster

可以在地圖上大幅縮減 Marker 數量的工具.

http://google-maps-utility-library-v3.googlecode.com/svn-history/r323/trunk/markerclustererplus/docs/reference.html#MarkerClustererOptions

Google Map InfoWindow 控制只顯示一個


" If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks).
Therefore, you may simply want to create one InfoWindow object just after you initialize your map, and then handle the click event handler as follows:
google.maps.event.addListener(curMarker, 'click', function() {
   infowindow.setContent(contentString);
   infowindow.open(map, curMarker);
});
Then the InfoWindow should automatically close when you click on a new marker without having to call the close() method. "

也就是說, 只要變數名稱一樣就好, API 會自動關掉其他同名的 InfoWindow.