Javascript 需要清除一个函数内存。jQuery函数运行次数过多
起初,我遇到了一个问题,点击事件多次触发,但我已经设法克服了这一问题,可能是过度使用了Javascript 需要清除一个函数内存。jQuery函数运行次数过多,javascript,jquery,function,memory-leaks,Javascript,Jquery,Function,Memory Leaks,起初,我遇到了一个问题,点击事件多次触发,但我已经设法克服了这一问题,可能是过度使用了unbind()和one(),您将在下面的代码中看到这一点 我这里有一些代码,它打开了一个通用的模式窗口,我用它来处理各种事情,在某些情况下包括密码表单 我认为你不需要HTML,所以我不会发布 当按钮或操作导致需要窗口时,我调用如下函数: showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis) $('#
unbind()
和one()
,您将在下面的代码中看到这一点
我这里有一些代码,它打开了一个通用的模式窗口,我用它来处理各种事情,在某些情况下包括密码表单
我认为你不需要HTML,所以我不会发布
当按钮或操作导致需要窗口时,我调用如下函数:
showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)
$('#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, '');
});
前三个变量决定窗口的外观,title
和html
确定内容和confirmThis
和denyThis
是在调用此函数之前设置的函数,如果这是一个confirm
窗口并且按下了confirm
或deny
按钮,则确定应该执行什么操作
在安全
窗口中,确认按钮被“签名”按钮替换,该按钮提交简单的密码表单并从数据库返回用户Id。如果成功返回用户Id,则脚本会按编程方式按下确认
按钮,然后根据对模式窗口初始打开的调用运行其功能
我的问题是如果输入了不正确的密码,或者用户取消了窗口,然后在没有刷新浏览器窗口的情况下,重新正确输入密码,则会执行两次confirmThis()
功能(或执行不正确的密码/取消操作的次数)
因此,很明显,它所做的是每次“记住”confirmThis
函数
正如我所说,最初,密码成功功能是单击两次confirmIt
,大量使用one()
解决了这个问题,现在它肯定只单击一次confirmIt
,但它仍然多次执行该功能
如何清除此功能并确保只执行一次?
从中调用模态窗口的函数如下所示:
showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)
$('#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");
};
});
});
根据我对
$.one
的理解,它只运行一次事件。如果将它绑定到事件两次,它将立即运行两次,但不再运行
示例:(单击按钮,它将运行事件4次)
每次单击saveDelivery,您实际上是在绑定另一个$。一个事件到#confirmIt
您可以做的是在模态函数(即,$)(“#confirmIt,#denyIt”)的开头从confirmIt和denyIt解除绑定事件。解除绑定('click');
,然后每次调用该函数时,您都会重新分配它们,而不是在它们之上构建。这并不理想,因为绑定/解除绑定比其他选项使用更多的资源,但不妨尝试一下从开始?根据我对$的理解。one
,它只运行一次事件。如果您将它绑定到e排气口,它将瞬间运行两次,但不会再运行
示例:(单击按钮,它将运行事件4次)
每次单击saveDelivery,您实际上是在绑定另一个$。一个事件到#confirmIt
您可以做的是在模态函数(即,$)(“#confirmIt,#denyIt”)的开头从confirmIt和denyIt解除绑定事件。解除绑定('click');
,然后每次调用该函数时,您都会重新分配它们,而不是在它们上面构建。这并不理想,因为绑定/解除绑定比其他选项使用更多的资源,但请尝试从这一点开始?很好!非常感谢,只需将该行添加到模态函数的顶部即可修复。我将尝试删除它g其他一些unbind()
调用也是如此,因为它们可能不需要!已排序!删除了所有其他unbind()
引用,现在它工作得很好!DNice one:)!很高兴我能提供帮助!注意!非常感谢,只需将该行添加到模态函数的顶部即可修复它。我还将尝试删除一些其他unbind()
调用,因为它们可能不需要!已排序!已删除所有其他unbind()
引用,现在它工作正常了!:DNice one:)!很高兴我能帮忙!