[英]Pause function while another function finishes
Im struggling with this code: 我正在努力解决这个问题:
Within 'swapFunc' function, I append an addEventListener to 'tEleBaby' element, immediately after which, I trigger the addEventListener by replacing the class of 'tEleBaby'. 在'swapFunc'函数中,我将addEventListener附加到'tEleBaby'元素,之后,我通过替换'tEleBaby'类来触发addEventListener。
The problem is with the code within 'setTimeout', which needs to run after the 'animListener' function has completed. 问题在于'setTimeout'中的代码,它需要在'animListener'函数完成后运行。 Im not keen on using setTimeout so would prefer a more sensible / correct way of dealing with this. 我不热衷于使用setTimeout,所以更喜欢更明智/更正确的处理方法。
swapFunc: function swapFunc(tEle, swapEle) {
var tEle = document.getElementById(tEle);
var tEleBaby = tEle.children[0];
tEleBaby.addEventListener("animationend", this.animListener, false);
tEleBaby.classList.add("animFadeOut");
// I want to remove the setTimeout; i.e. the animListener func should feedback to swapFunc
setTimeout(function () {
tEle.id = swapEle;
tEle.setAttribute("data-action", dataAction);
tEle.setAttribute("data-tooltip", dataTooltip);
}, 500);
},
animListener: function animListener(ev) {
if (ev.type.toLowerCase().indexOf("animationend") >= 0) {
var eventTarget = ev.target;
eventTarget.className = "animFadeIn cFBIcons";
}
},
Try it this way : 试试这种方式:
swapFunc: function swapFunc(tEle, swapEle) {
var tEle = document.getElementById(tEle);
var tEleBaby = tEle.children[0];
tEleBaby.addEventListener("animationend", listener, false);
tEleBaby.classList.add("animFadeOut");
function listener(ev) {
animListener(ev, callMe);
}
function callMe() {
tEle.id = swapEle;
tEle.setAttribute("data-action", dataAction);
tEle.setAttribute("data-tooltip", dataTooltip);
}
},
animListener: function animListener(ev, callback) {
if (ev.type.toLowerCase().indexOf("animationend") >= 0) {
var eventTarget = ev.target;
eventTarget.className = "animFadeIn cFBIcons";
}
callback();
},
Why don't you simply put that code in the event handler? 为什么不简单地将该代码放在事件处理程序中?
var self = this;
tEleBaby.addEventListener ("animationend" , function (ev) {
self.animListener(ev);
//rest of code
} , false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.