解决:
需要运行在 chrome 及其他支持ES2015的浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!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> |
参考: