2019年4月20日 | Leave a comment 解决: <!DOCTYPE html> <html> <head> <script> let i = true; function get() { const p = new Promise((resolve, reject) => { if (i) { resolve("ok"); } else { reject("error"); } i = !i; }); return p; } function onRefresh() { const p = get(); let resultDiv = document.getElementById("result"); let errorDiv = document.getElementById("error"); p.then(v => { resultDiv.innerText = v; errorDiv.innerText = null; }).catch(err => { resultDiv.innerText = null; errorDiv.innerText = err; }); } </script> </head> <body> <div id="result"></div> <div id="error"></div> <button onclick="onRefresh()">刷新</button> </body> </html> 123456789101112131415161718192021222324252627282930313233343536 <!DOCTYPE html><html> <head> <script> let i = true; function get() { const p = new Promise((resolve, reject) => { if (i) { resolve("ok"); } else { reject("error"); } i = !i; }); return p; } function onRefresh() { const p = get(); let resultDiv = document.getElementById("result"); let errorDiv = document.getElementById("error"); p.then(v => { resultDiv.innerText = v; errorDiv.innerText = null; }).catch(err => { resultDiv.innerText = null; errorDiv.innerText = err; }); } </script> </head> <body> <div id="result"></div> <div id="error"></div> <button onclick="onRefresh()">刷新</button> </body></html> 参考: