![](/img/trans.png)
[英]Why can't I add an event listener to each of my elements in my array, not just the last one when using knockout js
[英]Why my event listener won't last after one event when using Promise in ES6?
我有以下代码(一个简单的代码):
码:
var myPromise = new Promise(function(success, error) { document.getElementById("success").addEventListener("click", function() { success({ msg: "This is success", code: 100 }); }); document.getElementById("error").addEventListener("click", function() { error({ msg: "This is error", code: 10 }); }); }); console.log("Hello"); myPromise.then(function(content) { console.log(content.msg); }); myPromise.catch(function(content) { console.log(content.code); });
<button id="success">Success</button> <button id="error">Error</button>
首次单击两个按钮之一将记录“ This is success
或“ 10
。
为什么第二次,第三次及以后两次单击两个按钮都不会在控制台窗口中记录任何内容 ? 为什么事件监听器似乎突然消失了?
仅供参考:我目前正在ES6中尝试Promise,以了解其工作原理。
谢谢。
仅提供了Error / Success函数,不会再调用EventListener。 我添加了console.info
语句以显示正在调用EventListener。
var myPromise = new Promise(function(success, error) { document.getElementById("success").addEventListener("click", function() { console.info("called success" ); success({ msg: "This is success", code: 100 }); }); document.getElementById("error").addEventListener("click", function() { console.info("called error" ); error({ msg: "This is error", code: 10 }); }); }); console.log("Hello"); myPromise.then(function(content) { console.log(content.msg); }); myPromise.catch(function(content) { console.log(content.code); });
<button id="success">Success</button> <button id="error">Error</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.