![ECMAScript Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/58/36700058/b_36700058.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it...
- Create a new working directory, navigate into it with your command-line application, and start the Python SimpleHTTPServer.
- Create a file named rocket.js that exports the name of a rocket, a countdown duration, and a launch function:
export default name = "Saturn V"; export const COUNT_DOWN_DURATION = 10; export function launch () { console.log(`Launching in ${COUNT_DOWN_DURATION}`); launchSequence(); } function launchSequence () { let currCount = COUNT_DOWN_DURATION; const countDownInterval = setInterval(function () { currCount--; if (0 < currCount) { console.log(currCount); } else { console.log('LIFTOFF!!! '); clearInterval(countDownInterval); } }, 1000); }
- Create a file named main.js that imports from rocket.js, logs out details, and then calls the launch function:
import rocketName, {COUNT_DOWN_DURATION, launch } from './rocket.js'; export function main () { console.log('This is a "%s" rocket', rocketName); console.log('It will launch in "%d" seconds.', COUNT_DOWN_DURATION); launch(); }
- Next, create an index.html file that imports the main.js module and runs the main function:
<html> <head> <meta charset='UTF-8' /> </head> <body> <h1>Open your console.</h1> <script type="module"> import { main } from './main.js'; main(); </script> </body> </html>
- Open your browser and then the index.html file. You should see the following output:
![](https://epubservercos.yuewen.com/C944E7/19470395501577006/epubprivate/OEBPS/Images/3be6b910-02e5-4259-b3e3-85233e7fa412.png?sign=1739298478-5T8eDnvMPB8oh7o8ngBTgEC6HCUz3e7i-0-8c4ce85d9671d3844089220efe192738)