Javascript SetTimeOut函数以某种方式不起作用

时间:2012-07-11 14:47:39

标签: javascript settimeout

我的系统每隔500毫秒填充一次数据,而我的屏幕彼此之间充满了html表格。每个单元格都有唯一的键属性。无论如何,我正在缓存所有这些。

我有一个全局JavaScript对象(_cellColorTimeouts),它包含我上面提到的tableRows的cellElements的settimeout函数。在缓存单元格之后,系统会创建超时函数,用于擦除特定单元格的css(在3000毫秒内)。

在下面的代码块中,uiElementKey_X和uiElementKey_Y完全相同,但缓存方式不同。在表id中添加唯一后缀会使它们不同。此过程也适用于行和单元格项目。

_cellColorTimeouts数据的示例是

//array object keys are names of unique cell items.
_cellColorTimeouts = [uiElementKey_X_1, uiElementKey_X_2, uiElementKey_X_3,
                      uiElementKey_Y_1, uiElementKey_X_2, uiElementKey_Y_3];

. 
. //does somethings to change cell colour
.

    //after 3 seconds i need to clear css of this cell without looping the dom so i do it via cached dom.
    if (_cellColorTimeouts.hasOwnProperty(uiElementKey) && _cellColorTimeouts[uiElementKey] != null) {
         clearTimeout(_cellColorTimeouts[uiElementKey]);
         _cellColorTimeouts[uiElementKey] = null;
       }
       _cellColorTimeouts[uiElementKey] = setTimeout(function () {
               clearColourOfCell(cell);
       }, 3000);
}

function clearColourOfCell(cell) {
    cell.style.backgroundColor = cell.rowBGColour;
    cell.style.color = "black";
    _cellColorTimeouts[cell.uiElementKey] == null;
    clearTimeout(_cellColorTimeouts[cell.uiElementKey]);
}

所以问题是settimeout函数不能用于第一个表,但第二个表格完全正常。我已检查是否有来自全局的任何settimeout函数返回id,是的。对于第一个表,它不起作用。我知道这个问题对我的案子来说太独特了,但是任何想法都会被限制吗?

----编辑----全功能UNCUT VERSION -----

function setWidgetData(widgetId, rowId, colId, value, colIndex) {
"use strict";

// check colIndex
if (colIndex === undefined || colIndex === null) {
    colIndex = 0;
}

// loop on ui tables
var uiTables = _widgetUIElements[widgetId];
//var timeout;
for (var tableId in uiTables) {
    var uiTable = uiTables[tableId];
    var uiElementKey = tableId + "#" + rowId + "#" + colId + "#" + colIndex;
    var cellCachedObject = uiTable[uiElementKey];

    // check cell
    if (cellCachedObject == undefined) {
        //console.log("cell is undefined : " + "widgetId : " + widgetId + " - " + "rowId : " + rowId + " - " + "colId : " + colId + " - " + "colIndex : " + colIndex);
    }
    else {
        // get cell
        var cell = cellCachedObject["domElement"];
        // set sell value
        var cellValue = value;

        // is value numeric? it means we will make some conversions on value
        if (isNumeric(cellValue)) {
            var canPaint = false;
            // check cell entity
            switch (cellCachedObject["entity"]) {
                // date-time?           
                case "DATETIME":
                    // convert unix date time to readable date time
                    cellValue = new Date(fixDecimalSeparator(cellValue) * 1000);
                    cellValue = fixDateTimeDigits((cellValue.getDate())) + "/" + fixDateTimeDigits((cellValue.getMonth() + 1)) + " " + fixDateTimeDigits(cellValue.getHours()) + ":" + fixDateTimeDigits(cellValue.getMinutes());
                    break;
                // date?           
                case "DATE":
                    // convert unix date time to readable date time
                    cellValue = new Date(fixDecimalSeparator(cellValue) * 1000);
                    cellValue = fixDateTimeDigits((cellValue.getDate())) + "/" + fixDateTimeDigits((cellValue.getMonth() + 1));
                    break;
                // numeric?          
                case "NR":
                    // fix "," character in value
                    cellValue = fixDecimalSeparator(cellValue);
                    //just format the presicion
                    cellValue = number_format(cellValue, cellCachedObject["precision"], '.', ',');
                    canPaint = true;
                    break;
                // other?           
                default:
                    // fix "," character in value
                    cellValue = fixDecimalSeparator(cellValue);
                    // if cell is number, no entity conversion
                    // entity convertion
                    cellValue = entityConverter(cellCachedObject["entity"], cellCachedObject["entityTo"], cellValue);
                    cellValue = new Number(cellValue).toFixed(cellCachedObject["precision"]);

                    // if widget currency is not USD. it means user selected currency from currency list or default user currency
                    if (cellCachedObject["isConvertable"]) {
                        // this scoop is not active with the new xml. if FOREX1 widget entity is RECIPCUR but never should not be
                        if (cellCachedObject["widgetIsFOREX1"]) {
                            cellValue = _currencyConverter.convertTrend(cellValue, cellCachedObject.currencyValueType, cellCachedObject["currencyTo"], cellCachedObject["rowId"], cellValue);
                        }
                        else {
                            cellValue = _currencyConverter.convert(cellValue, cellCachedObject["currency"], null, cellCachedObject["precision"]);
                        }
                    }
                    canPaint = true;
            }

            // if it is not date time
            if (canPaint) {
                // get current value of cell
                var currentValue = cell.getAttribute("currentValue");
                // check current value of cell make them coloured.
                if (currentValue !== undefined) {
                    // new value is bigger than old value
                    var newVal = parseFloat(value);
                    var oldVal = parseFloat(currentValue);
                    var rowBGColour = cellCachedObject["rowBGColor"];
                    cell.rowBGColour = rowBGColour;
                    cell.uiElementKey = uiElementKey;
                    if (newVal > oldVal) {
                        //cell.css({ "background-color": "Green", "color": "White" });
                        cell.style.backgroundColor = "green";
                        cell.style.color = "white";
                    }
                    // new value is smaller than old value
                    if (newVal < oldVal) {
                        //cell.css({ "background-color": "Red", "color": "White" });
                        cell.style.backgroundColor = "red";
                        cell.style.color = "white";
                    }
                    if (_cellColorTimeouts.hasOwnProperty(uiElementKey) && _cellColorTimeouts[uiElementKey] != null) {
                        clearTimeout(_cellColorTimeouts[uiElementKey]);
                        _cellColorTimeouts[uiElementKey] = null;
                    }
                    _cellColorTimeouts[uiElementKey] = setTimeout(function () {
                        return function () {
                            clearColourOfCell(cell);
                        };
                    } (cell), 3000);
                    newVal = oldVal = rowBGColour = null;
                }
                currentValue = null;
            }
            canPaint = null;

            // set new value as a current value
            cell.setAttribute("currentValue", value);
        }

        cell.innerHTML = '';
        cell.innerHTML = cellValue;

        cellValue = null;
    }

    uiTable = uiElementKey = cellCachedObject = null;
}

uiTables = null;
}

1 个答案:

答案 0 :(得分:1)

你没有发布足够的代码让我知道这是问题所在,但这是一个不错的选择:

   _cellColorTimeouts[uiElementKey] = setTimeout(function () {
      return function() {
           clearColourOfCell(cell);
      };
   }(cell), 3000);

通过设置这样的超时处理程序,您可以确保处理程序具有自己的“cell”变量的私有副本,这样无论处理程序之前如何更改“单元格”最后调用,该副本将保留正确的值。