diff --git a/README.md b/README.md index ed7e0e6..e08f8fd 100644 --- a/README.md +++ b/README.md @@ -127,16 +127,23 @@ Quick screenshots per second by reusing context ```ts import { createContext, destroyContext, domToPng } from 'modern-screenshot' -createContext(document.querySelector('#app')).then(context => { - setInterval(() => { +async function screenshotsPerSecond() { + const context = awiat createContext(document.querySelector('#app')) + for (let i = 0; i < 10; i++) { domToPng(context).then(dataUrl => { const link = document.createElement('a') - link.download = 'screenshot.png' + link.download = `screenshot-${ i + 1 }.png` link.href = dataUrl link.click() + if (i + 1 === 10) { + destroyContext(context) + } }) - }, 1000) -}) + await new Promise(resolve => setTimeout(resolve, 1000)) + } +} + +screenshotsPerSecond() ``` See the [context.ts](src/context.ts) diff --git a/README.zh-CN.md b/README.zh-CN.md index 6e23ae8..8783038 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -127,16 +127,23 @@ DOM 转 HTMLElement ```ts import { createContext, destroyContext, domToPng } from 'modern-screenshot' -createContext(document.querySelector('#app')).then(context => { - setInterval(() => { +async function screenshotsPerSecond() { + const context = awiat createContext(document.querySelector('#app')) + for (let i = 0; i < 10; i++) { domToPng(context).then(dataUrl => { const link = document.createElement('a') - link.download = 'screenshot.png' + link.download = `screenshot-${ i + 1 }.png` link.href = dataUrl link.click() + if (i + 1 === 10) { + destroyContext(context) + } }) - }, 1000) -}) + await new Promise(resolve => setTimeout(resolve, 1000)) + } +} + +screenshotsPerSecond() ``` 请查看 [context.ts](src/context.ts)