【问题标题】:call back function set by JS settimeout doesn't work in IE8JS settimeout设置的回调函数在IE8中不起作用
【发布时间】:2010-06-02 10:13:50
【问题描述】:
<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>  

 <script>  

 // 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> 

右键单击 div 区域时,在 IE 和 FF 中的行为不同。在 ff 中 display_short 将每 1 秒调用一次,即使您不释放右键单击,但在 IE 中,如果您不释放右键单击,它将不会调用 display_short。

但我希望在 IE 中它应该每 1 秒调用一次 display_short,如果你也不释放的话。 有什么解决办法吗?

【问题讨论】:

  • 在两种浏览器中对我来说都是一样的。 oncontextmenu 仅在右键释放时触发,而不是按下。您对clearTimeout() 的使用毫无意义,因为您没有传入计时器句柄(由setTimeout 函数返回);无论如何,如果超时已经触发,则不需要清除它。

标签: javascript


【解决方案1】:

变量我; 我 = 0; 函数循环() { 我 = 我 + 1; 警报(字符串(i)); setTimeout("循环()",1000); } setTimeout("循环()",1000);

你好

请在 IE8 中尝试上面的代码,直到您不释放右键单击才会发出警告消息。

但在Firefox中,即使您不释放右键,它也会发出警告消息。

我想要 IE8 中的 firefox 功能。

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 2012-04-07
    • 2012-06-28
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多