开发者

call back function set by JS settimeout doesn't work in IE8

开发者 https://www.devze.com 2023-01-02 12:39 出处:网络
<html> <head> <script> function addEvent( obj, type, fn ) { if ( obj.attachEvent ) { obj[\'e\'+type+fn] = fn;
<html>  
 <head>  
 <script>  
 function addEvent( obj, type, fn ) {  

   if ( obj.attachEvent ) {  

     obj['e'+type+fn] = fn;  

     obj[type+fn] = function(){obj['e'+type+fn]( window.event );}  

     obj.attachEvent( 'on'+type, obj[type+fn] );  

   } else 

     obj.addEventListener( type, fn, false );  

 }  

 </script>  

 </head>  

 <body>  

 <!-- HTML for example event goes here -->  

 <div id="mydiv" style="border: 1px solid black; width: 100px; height: 100px; margin-top: 10px;"></div>  

 <s开发者_如何学编程cript>  

 // Script for example event goes here  

 addEvent(document.getElementById('mydiv'), 'contextmenu', function(event) {  
     display_short('right-clicked (contextmenu)');  
 });  

 function display_short(str)  
 {  
     clearTimeout();  
     document.getElementById('mydiv').innerHTML = str;  
     if (str != '')  
         alert("hello");
         setTimeout("display_short('abcd')", 1000);  

 }  
 </script>  
 </body>  
 </html> 

Behaves diffrently in IE and FF when you right click on the div area. In ff display_short will be called for evry 1 sec eventhough you dont release right click but in IE it will not call display_short if you dont release right click.

But i expect in IE it should call display_short for every 1 sec if you dont release also. Is there any solution for this?


var i; i = 0; function loop() { i = i + 1; alert(String(i)); setTimeout("loop()",1000); } setTimeout("loop()",1000);

hello

Please try the above code in IE8 it will not give alert message until you dont release right click.

But in firefox it will give alert message even though if you dont release the right click.

I want the firefox functionality in IE8.

0

精彩评论

暂无评论...
验证码 换一张
取 消