0

Good evening, I have a javascript function to set background color, etc when user onclick the table row. The function does not perform correctly unless it is called twice or when I press F12 for development tools, similar situation as Function doesn't correctly perform unless it is called twice.

I managed to handle it as below but the problem is that, when the user onclick the table row, it needs around 2-3 seconds for the background color to be change. How can I reduce the time for the function to perform twice?

JavaScript

    <SCRIPT LANGUAGE="JavaScript"> 
         setBackGroundColorOnIE (tableRowNumber) {
              //........
              //........
              setBackGroundColorOnIE (tableRowNumber)//I need the function to perform twice in one call
         }
    </SCRIPT>

I know it is a bad practice, but I'm really have no idea on how to fixed the compability issues in IE9. So, I came out with something like this. Need some hints and advices, thanks in advanced.

4

2 回答 2

0

您可以添加一些条件来检查您的函数是否需要再次运行,例如:

function setBackGroundColorOnIE (tableRowNumber) {   
   //check if your condition is met
   if( some_condition) {
       var timerId = setTimeout(function() {
        setBackGroundColorOnIE (tableRowNumber);
       }, 5000); //set to 5 seconds
   }
   else {
       clearTimeout ( timerId );
   }
}
//call the function
setBackGroundColorOnIE(some_value); 
于 2013-09-05T03:54:59.743 回答
0

一个简单的解决方案是包含 jquery ui,它允许您更改颜色,然后在颜色更改完成时进行回调。这是一个带有 jquery 和 jquery ui 的 JS fiddle。http://jsfiddle.net/kqMs9/

$(function(){
$('button').on('click', function(){
    $('.background').animate({
        backgroundColor: '#000'
    }, 1500, function(){ alert('background-color changed!');});

});

});

于 2013-09-05T03:54:17.003 回答