2019年4月20日 | Leave a comment 解决: 需要运行在 chrome 及其他支持ES2015的浏览器 <!DOCTYPE html> <html> <head> <script> function* fib() { let f1 = 1, f2 = 2; yield f1; yield f2; while (true) { let ret = f1 + f2; f1 = f2; f2 = ret; yield ret; } } let f = fib(); function increase() { let v = f.next(); console.log(v); let list = document.getElementById("list"); list.append(`<li>${v.value}<li>`); } function onLoad() { setInterval(increase, 1000); } </script> </head> <body onload="onLoad()"> <ul id="list"></ul> </body> </html> 1234567891011121314151617181920212223242526272829303132 <!DOCTYPE html><html> <head> <script> function* fib() { let f1 = 1, f2 = 2; yield f1; yield f2; while (true) { let ret = f1 + f2; f1 = f2; f2 = ret; yield ret; } } let f = fib(); function increase() { let v = f.next(); console.log(v); let list = document.getElementById("list"); list.append(`<li>${v.value}<li>`); } function onLoad() { setInterval(increase, 1000); } </script> </head> <body onload="onLoad()"> <ul id="list"></ul> </body></html> 参考: