loopするんじゃね? gaina Follow 2011-12-26 14:22:51 License: MIT License Fork0 Fav0 View191 Play Stop Reload Fullscreen Smart Phone Readme JavaScript 58 lines HTML 2 lines CSS 1 lines loopするんじゃね? function GIterator(arr){ var $arr = arr || [], $index = 0, $len = $arr.length; return { current:function(){ return $arr[$index]; }, next:function(){ if(!this.hasNext()){ return null; } $index += 1; return this.current(); }, prev:function(){ if(!this.hasPrev()){ return null; } $index -= 1; return this.current(); }, hasNext:function(){ return $index < $len - 1; }, hasPrev:function(){ return $index > 0; }, reset:function(){ $index = 0; return this.current(); }, last:function(){ $index = $len - 1; return this.current(); } }; } var t = new GIterator([1,2,3,4,5,6]); var b = document.getElementById("btn"); var r = document.getElementById("results"); r.innerHTML = t.current(); if(document.addEventListener){ b.addEventListener("click",pushButton,false); }else if(document.attachEvent){ b.attachEvent("onclick",pushButton); }else{ b.onclick = pushButton; } function pushButton(){ r.innerHTML = t.next() || t.reset(); } <div id="results">00000</div> <button id="btn">PUSH</button> loopするんじゃね? body { background-color: #DDDDDD; font: 30px sans-serif; } function GIterator(arr){ var $arr = arr || [], $index = 0, $len = $arr.length; return { current:function(){ return $arr[$index]; }, next:function(){ if(!this.hasNext()){ return null; } $index += 1; return this.current(); }, prev:function(){ if(!this.hasPrev()){ return null; } $index -= 1; return this.current(); }, hasNext:function(){ return $index < $len - 1; }, hasPrev:function(){ return $index > 0; }, reset:function(){ $index = 0; return this.current(); }, last:function(){ $index = $len - 1; return this.current(); } }; } var t = new GIterator([1,2,3,4,5,6]); var b = document.getElementById("btn"); var r = document.getElementById("results"); r.innerHTML = t.current(); if(document.addEventListener){ b.addEventListener("click",pushButton,false); }else if(document.attachEvent){ b.attachEvent("onclick",pushButton); }else{ b.onclick = pushButton; } function pushButton(){ r.innerHTML = t.next() || t.reset(); } <div id="results">00000</div> <button id="btn">PUSH</button> body { background-color: #DDDDDD; font: 30px sans-serif; } use an iframe compat browser, deer Tweet QR code Embed Design view Code view <script type="text/javascript" src="http://jsdo.it/blogparts/isBg/js?view=design"></script><p class="ttlBpJsdoit" style="width: 465px; margin: 0; text-align: right; font-size: 11px;"><a href="http://jsdo.it/gaina/isBg" title="loopするんじゃね?">loopするんじゃね? - jsdo.it - share JavaScript, HTML5 and CSS</a></p> zip tags Tweet twitter