在另一个功能完成时暂停功能
Pause function while another function finishes
我正在与以下代码作斗争:
在"swapFunc"函数中,我将addEventListener附加到"tEleBaby"元素,然后立即通过替换"tEleBaby"类来触发addEventListener。
问题出在"setTimeout"中的代码上,该代码需要在"animListener"函数完成后运行。我不喜欢使用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";
}
},
这样试试:
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();
},
为什么不简单地将代码放在事件处理程序中?
var self = this;
tEleBaby.addEventListener ("animationend" , function (ev) {
self.animListener(ev);
//rest of code
} , false);
相关文章:
- Facebook股票计数,暂停直到回调功能结束
- 是否有“”的键盘快捷键;暂停动画”;Chrome开发工具中的功能?(F12)
- 暂停和取消暂停功能中的音频
- 故意暂停执行功能
- 停止/暂停/禁用第三个功能的 2 个功能
- 使用一个功能播放/暂停多个视频
- 音轨 JS 功能无法播放或暂停音频
- 我如何使因果报应;s的自动监视功能在运行测试前暂停
- 如何向正在运行的setInterval添加暂停/播放功能
- 禁用单击Fotorama滑块上的暂停功能
- 在另一个功能完成时暂停功能
- 为滑块添加暂停功能
- 元素悬停时的暂停功能
- 用于音频的 Flash HTML5 画布条件.暂停和恢复功能
- 我将如何获取此.scrollTop代码并对其进行修改以在鼠标悬停或滚动时暂停其功能
- 如何在父iframe中暂停html5音频,该功能来自子框架
- 切换HTML按钮暂停和恢复功能
- 我可以暂停滚动功能吗?
- 增加了一个暂停功能,不能工作.有人知道解决方法吗?
- Javascript滑动条暂停功能在悬停