ãã£ã¨ã¨ã¬ã¬ã³ãã«setTimeoutãã¡ã½ããåããæ¹æ³
â»è¿½è¨ï¼ãã¾ãã«ãè¨äºã¿ã¤ãã«ãé©å½ããã¦æãªããæ¥æ¬èªã§ãï½ã¨æã£ãã®ã§ã¿ã¤ãã«ãå¤ãã¾ããã
http://labs.cybozu.co.jp/blog/kazuho/archives/2006/12/oo-settimeout.php
http://blog.livedoor.jp/dankogai/archives/50714622.html
ãã®ãã¿ã¯malaããã1å¹´ååã«ééããå°ç¹ãªã®ã§ããç¨åº¦å¸¸èã®é¨é¡ãã¨æã£ã¦ã¾ããã
ã§ãdankogaiãããããããã£ã¦ãã¨ã¯ç¥ããªã人ãå¤ããã ããmalaãããåããã¿ãã¾ãããããã«ãæããªãã®ã§ãã¤ãã§ã«LDRã«ç¾å¨ä½¿ããã¦ãFunction.prototype.laterã¨ããç´¹ä»ãã¦ã¿ãã
Function.prototype.later = function(ms){ var self = this; return function(){ var args = arguments; var thisObject = this; var res = { complete: false, cancel: function(){clearTimeout(PID);}, notify: function(){clearTimeout(PID);later_func()} }; var later_func = function(){ self.apply(thisObject,args); res.complete = true; }; var PID = setTimeout(later_func,ms); return res; }; };
later()ã®æ»ãå¤ã§ãä¸æã¨ãå³å®è¡ã¨ããå¦çãããªãã¸ã§ã¯ããè¿ãã¦ã¾ãã
dankogaiã®ããæ¹ã ã¨ãªãã¸ã§ã¯ãã®timeoutããããã£ãæ±æãã¡ãããã©ããã¡ãã®ããæ¹ã¯setTimeout()ã®æ»ãå¤ãã¯ãã¼ã¸ã£å
ã§å¦çãã¦ãã¦æ±æãå°ãªãã®ãã¨ã¬ã¬ã³ãã
使ãã¨ãã¯ãããªæã
function example(msg){ alert(msg); } //ããã //é 延å®è¡ var task = example.later(1000)('hello'); //ããã //ãã£ã³ã»ã« task.cancel(); //ããã //ã¾ã å®è¡ãã¦ãªãã£ããããã£ã±ãå³å®è¡ if(!task.complete){ task.notify(); }
ãã£ã³ã»ã«ã¨ãå³å®è¡ã¨ãçµäºç¢ºèªãå¿
è¦ãªããã°later()ã®æ»ãå¤ãç¡è¦ããã ãã
人æ§ã®æ¸ããã½ã¼ã¹ã®è§£èª¬ã§ã¡ãã£ã¨ç³ã訳ãªãã§ãããLDRã®ã½ã¼ã¹ã¯ãã®æã®ã¤ãã£ãªã ã®å®åº«ãªã®ã§èªãã¨åå¼·ã«ãªãã¾ãããã¨ã