var fnShowMessage = function() {
var obj = document.getElementById("MessageWnd");
var m_tmrShow = null;

if (!obj) { return; }

this.Show = function(szText, millSecond) {
clearInterval(m_tmrShow);

if (!IsNumeric(millSecond)) { millSecond = 3000; }

if (!this.IsHidden()) {
setText(szText);
m_tmrShow = setInterval(setHide, millSecond);
bPopupWindow = true;
return;
}

var intBodyWidth = document.body.offsetWidth;
var intBodyHeight = document.body.offsetHeight;

var intLeft = ((intBodyWidth - 250) / 2) + document.body.scrollLeft;
var intTop = ((intBodyHeight - 60) / 2) + document.body.scrollTop;

obj.style.left = intLeft + "px";
obj.style.top = intTop + "px";

setText(szText);

m_tmrShow = setInterval(setHide, millSecond);

bPopupWindow = true;
}

this.Hide = function() { setHide(); }

this.Text = function(szText) { setText(szText); }

this.IsHidden = function() { return (obj.style.top == "-9999px" && obj.style.left == "-9999px"); }

function setHide() {
alert("Test");
clearInterval(m_tmrShow);
obj.style.top = "-9999px";
obj.style.left = "-9999px";

setText();

bPopupWindow = false;
}

function setText(szText) {
var objText = null;

if (objText = document.getElementById("MessageText")) {
objText.innerText = IsEmpty(szText) ? "" : szText;
}
}
}这里有一部分的 JS 代码,作用是在网页中弹出一个小窗口(DIV)。
在 Show 中,检测一下这个小窗口是否隐藏,如果没有隐藏,则直接设置上面的提示文本!
如果隐藏,则重新弹出。现在我用 setInterval 或 setTimeout 来隔一段时间让这个小窗口自动消失!
当这个小窗口出现了一段时间后,的确消失了!但有一个问题,在 Show 函数里,如果小窗口本身没有消失而的话,那么就用 clearInterval 或 clearTimeout 来取消之前的计时器,然后设置它上面的提示文本,然后再重新计时!但是使用 clear*** 来清除计时器后,原计时器中的函数还会被执行一次,即是说它还会执行多一次 setHide() 函数。这是怎么回事呢?我表达能力不怎么好,有问题的请举手!

解决方案 »

  1.   

    问题解决了~~
    原来是调用的时候出了问题~~~
    我在另一个函数中,new 了一个新的对象,然后判断是否显示!
    问题就在这里,当旧的那个没隐藏,而我再次的调用了这个函数,所以又 new 了一个新的对象!我现在把这个 new 出来的对象放在了函数外,作为一个公共类!就这样解决了问题...