简体   繁体   English

如何停止多次调用 function

[英]How to stop function being called multiple times

I can't figure out why my code is triggering multiple calls of a function.我不知道为什么我的代码会触发 function 的多次调用。 I'm creating a calendar app and each time I delete an event, it increments the number of times the function gets called.我正在创建一个日历应用程序,每次删除事件时,它都会增加调用 function 的次数。 It must be something to do with the for loop which I've tried to resolve using 'break' but doesn't work.这一定与我尝试使用'break'解决但不起作用的for循环有关。

When the user opens a day in the calendar, it displays the events.当用户在日历中打开一天时,它会显示事件。 I have an event listener to trigger a modal of "Are you sure you want to delete event?".我有一个事件侦听器来触发“您确定要删除事件吗?”的模式。

// Delete event
delEvent = document.getElementsByClassName('eventEntry');
for (var e = 0; e < delEvent.length; e++) {
    delEvent[e].addEventListener('click', delete_event,false);
    break;
}
// Opens model to prompt event delete
function delete_event(d){
    id = d.currentTarget.id;
    $('#modal-delete').modal('open');
    delModalBtn = document.getElementsByClassName('delTrue');
    for (var i = 0; i < delModalBtn.length; i++) {
        delModalBtn[i].addEventListener('click', event_del_true,false);
        break;
    }
}

When the user confirms they want to delete the event, event_del_true function is called (code below).当用户确认他们想要删除事件时,调用 event_del_true function(代码如下)。 This technically works but the Toast popup component from MaterializeCSS displays multiple times.这在技术上是可行的,但 MaterializeCSS 中的 Toast 弹出组件会显示多次。

For example:例如:

  • Delete 1 event = event is deleted and popup is displayed.删除 1 个事件 = 事件被删除并显示弹出窗口。

  • Delete a 2nd event = the event is deleted but the popup displays twice, and so the cycle begins删除第二个事件 = 事件被删除但弹出窗口显示两次,因此循环开始

     function event_del_true(){ $.ajax({ type: "POST", url: "php/delete-event.php", data: {id: id} }).done(function() { M.toast({ html: 'Event deleted', classes: 'rounded', displayLength: 2000, inDuration: 375, outDuration: 300 }); });

Any help would greatly be appreciated, Kind Regards.任何帮助将不胜感激,亲切的问候。

Why do you need to add a loop here if you break the loop in the first iteration?如果在第一次迭代中中断循环,为什么需要在此处添加循环?

for (var i = 0; i < delModalBtn.length; i++) {
    delModalBtn[i].addEventListener('click', event_del_true,false);
    break;
}

Plus, every time you trigger the function, you are adding another event listener to the button, so the effect event_del_true will be added on top of each other every time you call delete_event另外,每次触发 function 时,都会向按钮添加另一个事件侦听器,因此每次调用delete_event时都会添加效果event_del_true

a simple fix would be moving this line out of the function一个简单的解决方法是将这条线移出 function

// Opens model to prompt event delete
function delete_event(d){
    id = d.currentTarget.id;
    $('#modal-delete').modal('open');
}

// add the event to every button with the class ONCE only
delModalBtn = document.getElementsByClassName('delTrue');
for (var i = 0; i < delModalBtn.length; i++) {
    delModalBtn[i].addEventListener('click', event_del_true,false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM