From bcd98fe1ad1d125aac715828f34049c573219c01 Mon Sep 17 00:00:00 2001 From: lana-k Date: Mon, 23 Aug 2021 22:20:03 +0200 Subject: [PATCH] add sleep function --- src/lib/utils/time.js | 6 +++++ .../Workspace/Tabs/Tab/DataView/index.vue | 27 +++++++++---------- .../Main/Workspace/Tabs/Tab/RunResult.vue | 20 +++++++------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/lib/utils/time.js b/src/lib/utils/time.js index 5858d15..39476a3 100644 --- a/src/lib/utils/time.js +++ b/src/lib/utils/time.js @@ -11,5 +11,11 @@ export default { clearTimeout(timeout) timeout = setTimeout(() => func.apply(this, arguments), ms) } + }, + + sleep (ms) { + return new Promise(resolve => { + setTimeout(() => { resolve() }, ms) + }) } } diff --git a/src/views/Main/Workspace/Tabs/Tab/DataView/index.vue b/src/views/Main/Workspace/Tabs/Tab/DataView/index.vue index 811cd73..7acda21 100644 --- a/src/views/Main/Workspace/Tabs/Tab/DataView/index.vue +++ b/src/views/Main/Workspace/Tabs/Tab/DataView/index.vue @@ -86,6 +86,7 @@ import PngIcon from '@/components/svg/png' import ClipboardIcon from '@/components/svg/clipboard' import cIo from '@/lib/utils/clipboardIo' import loadingDialog from '@/components/LoadingDialog' +import time from '@/lib/utils/time' export default { name: 'DataView', @@ -127,12 +128,11 @@ export default { nextTick allows you to do something after you have changed the data and VueJS has updated the DOM based on your data change, but before the browser has rendered those changed on the page. - Lees meer van Katinka Hesselink: http://www.hesselinkwebdesign.nl/2019/nexttick-vs-settimeout-in-vue/ + http://www.hesselinkwebdesign.nl/2019/nexttick-vs-settimeout-in-vue/ */ - setTimeout(() => { - this.$refs.viewComponent.saveAsPng() - }, 0) + await time.sleep(0) + this.$refs.viewComponent.saveAsPng() }, getOptionsForSave () { return this.$refs.viewComponent.getOptionsForSave() @@ -143,16 +143,15 @@ export default { this.$modal.show('prepareCopy') const t0 = performance.now() - setTimeout(async () => { - this.dataToCopy = await this.$refs.viewComponent.prepareCopy() - const t1 = performance.now() - if ((t1 - t0) < 950) { - this.$modal.hide('prepareCopy') - this.copyToClipboard() - } else { - this.preparingCopy = false - } - }, 0) + await time.sleep(0) + this.dataToCopy = await this.$refs.viewComponent.prepareCopy() + const t1 = performance.now() + if ((t1 - t0) < 950) { + this.$modal.hide('prepareCopy') + this.copyToClipboard() + } else { + this.preparingCopy = false + } } else { alert("Your browser doesn't support copying images into the clipboard. If you use Firefox you can enable it by setting dom.events.asyncClipboard.clipboardItem to true.") } diff --git a/src/views/Main/Workspace/Tabs/Tab/RunResult.vue b/src/views/Main/Workspace/Tabs/Tab/RunResult.vue index 9148ba1..15529cf 100644 --- a/src/views/Main/Workspace/Tabs/Tab/RunResult.vue +++ b/src/views/Main/Workspace/Tabs/Tab/RunResult.vue @@ -70,6 +70,7 @@ import IconButton from '@/components/IconButton' import csv from '@/lib/csv' import fIo from '@/lib/utils/fileIo' import cIo from '@/lib/utils/clipboardIo' +import time from '@/lib/utils/time' import loadingDialog from '@/components/LoadingDialog' export default { @@ -125,16 +126,15 @@ export default { this.$modal.show('prepareCSVCopy') const t0 = performance.now() - setTimeout(async () => { - this.dataToCopy = csv.serialize(this.result) - const t1 = performance.now() - if ((t1 - t0) < 950) { - this.$modal.hide('prepareCSVCopy') - this.copyToClipboard() - } else { - this.preparingCopy = false - } - }, 0) + await time.sleep(0) + this.dataToCopy = csv.serialize(this.result) + const t1 = performance.now() + if ((t1 - t0) < 950) { + this.$modal.hide('prepareCSVCopy') + this.copyToClipboard() + } else { + this.preparingCopy = false + } } else { alert("Your browser doesn't support copying images into the clipboard. If you use Firefox you can enable it by setting dom.events.asyncClipboard.clipboardItem to true.") }