<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6648236360472666945</id><updated>2012-05-07T02:51:46.812+08:00</updated><category term='網路交易'/><category term='CardGame'/><category term='Program'/><category term='網路遊戲'/><category term='星海爭霸2'/><category term='PSP'/><category term='NDS 攻略'/><category term='料理筆記'/><category term='VB.NET'/><category term='Apple'/><category term='SFC'/><category term='摩爾莊園'/><category term='Ajax'/><category term='C++'/><category term='Chrome'/><category term='DnD'/><category term='NDS資訊'/><category term='Mac'/><category term='Software'/><category term='精靈幻境系列'/><category term='Writing'/><category term='RO OpenKore'/><category term='FC'/><category term='PS'/><category term='星海爭霸2地圖'/><category term='摩爾勇士'/><category term='Culdcept'/><category term='生活相關'/><category term='遊戲設計'/><category term='RO'/><category term='限時免費APP'/><category term='BioInfo'/><category term='MHP3'/><category term='博弈遊戲'/><category term='LoL'/><category term='星海爭霸2雜錦'/><category term='Music'/><category term='RO JA.NET'/><category term='3DS'/><category term='遊戲新聞'/><category term='軟體相關'/><category term='模擬器'/><category term='雜談錄'/><category term='Java'/><category term='iOS開發'/><category term='動畫專區'/><category term='特價折扣'/><category term='ROM'/><category term='Php'/><category term='WebGame'/><category term='JavaScript'/><category term='遊戲王'/><category term='Blog'/><category term='魔法風雲會'/><title type='text'>GameLifeX</title><subtitle type='html'>　不只是遊戲，是生活</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://gamelifex.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/-/JavaScript'/><link rel='alternate' type='text/html' href='http://gamelifex.blogspot.com/search/label/JavaScript'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jason</name><uri>http://www.blogger.com/profile/00667801849417949181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://2.bp.blogspot.com/_BsYyM_o2_Pc/S8A0d9jcmJI/AAAAAAAAAOA/jkCxsQg-hCM/S220/%E7%B4%AF.bmp'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6648236360472666945.post-1304128486442518313</id><published>2010-09-25T22:53:00.000+08:00</published><updated>2010-09-25T22:53:30.702+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>setInterval 與 clearInteval 的問題！？</title><content type='html'>錯誤的用法:&lt;div&gt;var timer;&lt;/div&gt;&lt;div&gt;function xxxx(){&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&amp;nbsp;&amp;nbsp; window.ClearInterval(&lt;/span&gt;timer&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; timer = window.setInterval("abc()",1000);&lt;/div&gt;&lt;div&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;有一天我發現呼叫&lt;/span&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;clearInterval竟然無法停止之前的setInterval！&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;原來我把他們寫在同一行，導致了此問題，&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;以下文章寫到，&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="color: red;"&gt;"&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="color: red;"&gt;如果在setInterval所調用的那個函數(代號f)中執行clearInterval，該語句執行的效果似乎是被推遲到f函數執行的結束之後&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="color: red;"&gt;"&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;但實際上我的程式居然無視clearInterval，&lt;br /&gt;因此不但沒有停止之前的循環，還給我"多加一次"的循環！&lt;br /&gt;&lt;br /&gt;因此暫時解法如下:&lt;br /&gt;&lt;br /&gt;&lt;div&gt;var timer;&lt;/div&gt;&lt;div&gt;var firstrun = 0;&lt;/div&gt;&lt;div&gt;function xxxx(){&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&amp;nbsp;&amp;nbsp;//window.ClearInterval(&lt;/span&gt;timer&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="line-height: 20px;"&gt;&amp;nbsp;&amp;nbsp;if (firstrun==0){&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;timer = window.setInterval("abc()",1000);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;firstrun = 1;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;什麼！？就不要clearInterval？還用個鬼東西！firstrun？&lt;br /&gt;沒錯，由於同一行既clear又set，所以只好就無視啦！&lt;br /&gt;&lt;br /&gt;有其他解法就請待高人了...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;以下為參考文章:&lt;br /&gt;&lt;a href="http://dustyface.spaces.live.com/blog/cns!15ABFBE5008D2761!329.entry"&gt;來源: 網路&lt;/a&gt;&lt;br /&gt;--------------------&lt;br /&gt;setInterval：注意變量的生命期 | 使用外部function的變量作為判斷閾值&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #444444; font-family: Pmingliu, Verdana; font-size: 12px; line-height: 20px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="line-height: 20px; margin-bottom: 1.35em; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;setInterval有個有趣的現象。如果在setInterval所調用的那個函數(代號f)中執行clearInterval，該語句執行的效果似乎是被推遲到f函數執行的結束之後，如果是如上例21.，22. 行代碼，clearInterval的執行依賴有關的計數器或閥值變量-&amp;gt;如果那個變量(例如t)是f局部的，則系統每次都需要重新生成f的callobject，然後去和該callobject的局部變量相比較，決定是否執行clearInterval；即使達到了不再繼續調用f的條件，由於測試變量在f callobject內部，系統反覆生成f callobject執行clearInterval，下次再重複此動作; 當指令寄存器在執行inner function中的語句，允許inner function的指令去刪除系統對那個調用inner function的外部過程或函數的引用嗎？(暫理解為timer引用著系統在主線程中不斷調用inner f的那個定時器過程，clearInterval timer的內容之一是刪除timer對那個過程的引用)，或許這裡面因為循環引用或者是其他特殊的機制，至少在inner function的內部是不能執行的；因此當22語句的條件得以實行且是clearInterval這個特殊的情況,系統內部會做出特殊的標記,要在inner f執行完後執行清除定時器必要的工作。給我的感覺是，js process的中間代碼把上例中21., 22.行代碼優化成了同一個執行單元的中間代碼語句。並且當系統在執行inner f中的22句，系統重新獲得需要執行它的那些變量引用等等，不幸的是，這導致f的callobject重新被生成，而f內部的變量t每次都讀取外部的值，t早就超過了閾值，導致timer cleared對話框會不斷的被提示。&lt;/div&gt;&lt;div style="line-height: 20px; margin-bottom: 1.35em; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;例子中，nested function的outer function中定義t，判斷t可得到正確的行為,判斷2.1處的e.style.opacity也可以得到正確行為,因為e是在outer function中，並且依賴於js process 默認string to number的轉換成整數的行為，前9次都是0,第10次&amp;gt;=閥值，行為正確;但是使用在f內部聲明的t，或者2.2處，使用parseInt(e.style.opacity)都得不到正確的行為,特別是2.2處，parseInt(e.style.opacity)表達式生成一個f內部的匿名的變量,使用之後被js process丟棄該在f callobject中生成的匿名臨時變量和f callobject一樣準備GC,因此2.2處在if語句中使用parseInt來判斷表達式也得不到正確的行為。這點setIntveral的特殊性需留意。&lt;/div&gt;&lt;div style="line-height: 20px; margin-bottom: 1.35em; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;例子使用在outer function定義閥值var t的方法，每次通過parseInt來設置t，比2.1處，e雖然是外部變量但依賴默認的data type casting，2.1晦澀，且容易忽略setInterval的機制&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6648236360472666945-1304128486442518313?l=gamelifex.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gamelifex.blogspot.com/feeds/1304128486442518313/comments/default' title='張貼意見'/><link rel='replies' type='text/html' href='http://gamelifex.blogspot.com/2010/09/setinterval-clearinteval.html#comment-form' title='0 個意見'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/1304128486442518313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6648236360472666945/posts/default/1304128486442518313'/><link rel='alternate' type='text/html' href='http://gamelifex.blogspot.com/2010/09/setinterval-clearinteval.html' title='setInterval 與 clearInteval 的問題！？'/><author><name>Jason</name><uri>http://www.blogger.com/profile/00667801849417949181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://2.bp.blogspot.com/_BsYyM_o2_Pc/S8A0d9jcmJI/AAAAAAAAAOA/jkCxsQg-hCM/S220/%E7%B4%AF.bmp'/></author><thr:total>0</thr:total></entry></feed>
