ウィンドウのfocus、blurイベントを検知してカウントするサンプルを作成しました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<div></div> | |
<textarea onfocus="blurカウンタ.リセット()"></textarea> | |
<button onfocus="blurカウンタ.リセット()">ボタン</button> | |
</body> | |
<script> | |
blurカウンタ={ | |
値:0, | |
リセット:function(){ | |
this.swFocus = true | |
this.値 = 0 | |
}, | |
swFocus:true, | |
countup:function(){ | |
if(this.swFocus){return} | |
document.title = this.値++ | |
var this_=this | |
setTimeout( function(){this_.countup()}, 10 ) | |
}, | |
開始:function(){ | |
this.swFocus = false | |
this.countup() | |
} | |
} | |
var elem=document.body.firstChild | |
onfocus = function(){ | |
elem.innerText = 'focus' | |
blurカウンタ.リセット() | |
} | |
onblur = function(){ | |
elem.innerText = 'blur' | |
blurカウンタ.開始() | |
} | |
</script> | |
</html> |
ウィンドウ内のエレメントでもtextareaやbuttonなどでonfocusイベントが発生するとwindow.onblurが発生するので、フォームエレメントのonfocusイベント内からリセットしてあげる必要があります。
0 件のコメント:
コメントを投稿