0

I've got a little function which ables to handle pop-up windows on my page, and there is a little problem with clearTimeout. Please take a look at the code:

function toggleModal(et, delayed) {

        if(et.hasClass('acc-edit-fn')) {
            if($.browser.msie) {$('#opacity-modal').height($(document).height()).toggleClass('acc-edit-fn');
                }
            else {$('#opacity-modal').height($(document).height()).toggleClass('acc-edit-fn').fadeTo('fast', 0.5);
                }   
            et.html($(this).html()).toggleClass('acc-edit-fn');
            if(delayed > 0) {       
                var dtf = setTimeout(function(){et.toggleClass('acc-edit-fn'); }, delayed);
                var dts = setTimeout(function(){$('#opacity-modal').toggleClass('acc-edit-fn').removeAttr('style');}, delayed);          
            }   
        }
        else {
            $('#opacity-modal').toggleClass('acc-edit-fn').removeAttr('style');
            et.toggleClass('acc-edit-fn');  
            clearTimeout(dtf);
            clearTimeout(dts);
        }
    };

So basically if you are calling func with "delayed" > 0, it will call automatic close after defined value. But, if there is any button in pop-up which allows to close it before timer, pop-up will appear right after timer will be executed. I've tried to clear timeouts with clearTimeout(), but bug is still there. You can check it on fiddle here: http://jsfiddle.net/LvVu9/ just click red button, and then click "ok" button.

4

2 回答 2

2

dtf并且dts两者都超出了范围(您在哪里clearTimeout)。所以你可以定义这些全局的。

像这样的东西:

var dtf = null;
var dts = null;
function toggleModal(et, delayed) {

        if(et.hasClass('acc-edit-fn')) {
            if($.browser.msie) {$('#opacity-modal').height($(document).height()).toggleClass('acc-edit-fn');
                }
            else {$('#opacity-modal').height($(document).height()).toggleClass('acc-edit-fn').fadeTo('fast', 0.5);
                }   
            et.html($(this).html()).toggleClass('acc-edit-fn');
            if(delayed > 0) {       
                dtf = setTimeout(function(){et.toggleClass('acc-edit-fn'); }, delayed);
                dts = setTimeout(function(){$('#opacity-modal').toggleClass('acc-edit-fn').removeAttr('style');}, delayed);          
            }   
        }
        else {
            $('#opacity-modal').toggleClass('acc-edit-fn').removeAttr('style');
            et.toggleClass('acc-edit-fn');  
            clearTimeout(dtf);
            clearTimeout(dts);
        }
    };
于 2014-05-23T06:32:38.310 回答
0

设置为全局变量

var dtf,dts;

    if(dtf){
            clearTimeout(dtf);
            }
            if(dts){
            clearTimeout(dts);
            }
于 2014-05-23T06:32:35.733 回答