需要清除功能记忆。 jQuery运行函数太多次了

时间:2012-02-23 12:23:38

标签: javascript jquery function memory-leaks

最初,我遇到了点击事件多次触发的问题,但是我已经设法克服了这个问题,可能会过度使用unbind()one(),因为您会在我的代码中看到下面!

我这里有一些代码,它打开了一个普遍可用的模态窗口,我用它来处理各种事情,包括在某些情况下是密码表单。

我认为您不需要HTML,所以我不会发布。

当按钮或动作导致需要窗口时,我会调用这样的函数:

showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)

前三个变量确定窗口的外观,titlehtml确定内容,confirmThisdenyThis是在调用此函数之前立即设置的函数,如果这是一个confirm窗口并按下confirmdeny按钮,请确定应该采取的操作。

security窗口的情况下,确认按钮被“签名”按钮替换,该按钮提交简单的密码表单并从数据库返回用户ID。如果成功返回用户ID,则脚本以编程方式按下confirm按钮,然后按照模态窗口的初始打开调用运行它的功能。

我的问题是如果输入了错误的密码,或者用户取消了该窗口,之后又没有刷新浏览器窗口,请正确地重新输入密码,confirmThis()功能执行两次(或执行错误密码/取消操作的次数)。

所以,显然,它正在做的是每次“记住”confirmThis函数。

正如我所说,最初,密码成功功能是点击confirmIt两次,大量使用one()修复此问题,现在肯定只点击confirmIt一次,但它是仍然多次执行该功能。

如何清除此功能并确保仅执行一次?

我调用模态窗口的函数如下所示:

    $('#saveDelivery').click(function () {
           function confirmIt() {
                formData = (JSON.stringify($('#delDetail').serializeObject()));
                saveData(formData);
                $('#saveDelivery').removeClass('centreLoader');
            };
            showModalAlert('security', '300px', '185px', 'Security!', 'You need to "Sign" this action.', confirmIt, '');
    });

只需单击saveDelivery元素,此时就会声明confirmThis函数并提交一个AJAX表单

实际的showModalAlert功能如下:

function showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)        {

// stuff that opens the alert window \\

if (confirmThis == '') {
    $('#confirmIt').one('click', function () { $('#closeAlert').one('click').click(); });
} else {
    $('#confirmIt').one('click', function () { confirmThis(); $('#closeAlert').one('click').click(); });
};
if (denyThis == '') {
    $('#denyIt').one('click', function () { $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
} else {
    $('#denyIt').one('click', function () { denyThis(); $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
};   

if (type == "confirm") {
    $('.closeAlert, .signItForm').hide();
};
if (type == "alert") {
    $('.alertConfirm, .signItForm').hide();
};
if (type == "fixedAlert") {
    $('.closeAlert, .alertConfirm, .signItForm').hide();
};
if (type == "security") {
    $('.signItForm').show();
    $('.closeAlert').hide();
    $('#confirmIt').hide();
    $('#signIt').unbind().fadeTo('fast',1);
};
};

$('#signIt').live('click', function () {
var formData = (JSON.stringify($('.secureSign').serializeObject()));
var signitPwd = $('#signItpwd').val();
var jsonURL = "/jsonout/getdata.aspx?sql=SELECT id, password FROM users WHERE password ='" + signitPwd + "' LIMIT 1&output=json&usedb=new&labelName=any&fileName=";

$.getJSON(jsonURL, function (data) {
    if (data.length > 0) {
        $('.savingUserID').val(data[0].id);
        $('#confirmIt').one('click').click();
        $('#signIt').fadeTo('fast', 0);
        $('#confirmIt').show();
    } else {
        $('#signIt').fadeTo('fast', 0);
        $('#confirmIt').one('click').show();
        $('.closeAlert').show();
        $('.alertConfirm, .signItForm').hide();
        $('#alertTitle').html("Error!");
        $('#alertContent').css({ 'text-align': 'center' }).html("Password Denied");
    };
});
});

1 个答案:

答案 0 :(得分:1)

根据我对$.one的理解,它只运行事件ONCE。如果将它绑定到事件两次,它将立即运行两次,但不会再运行。

示例:http://jsfiddle.net/qCwMH/(单击按钮,它将运行该事件4次)。

每次点击saveDelivery,您都会将另一个$.one事件绑定到#confirmIt

你可以做的是在模态函数的开头(即$('#confirmIt, #denyIt').unbind('click');)将你的事件从confirmIt和denyIt解除绑定,然后你会在每次调用该函数时将它们分配给新的,而不是建立在他们。不理想,因为绑定/解除绑定比其他选项使用更多的资源,但只是尝试从一开始尝试?