Skip to content

Commit

Permalink
Feat: Pause timer on hover. (ankurk91#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
notarun authored and Resonious committed Dec 13, 2021
1 parent 3ec9a2d commit 8d31285
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/js/Component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
v-show="isActive"
class="toast"
:class="[`toast-${type}`, `is-${position}`]"
@mouseover="mouseover = true"
@mouseleave="mouseover = false"
@click="whenClicked">
<div class="toast-icon"></div>
<p class="toast-text" v-html="message"></p>
Expand All @@ -15,7 +17,7 @@
</template>

<script>
import {removeElement} from './helpers.js';
import {removeElement, Timer} from './helpers.js';
import Positions from './positions.js'
import eventBus from './bus.js'
Expand Down Expand Up @@ -62,6 +64,7 @@
isActive: false,
parentTop: null,
parentBottom: null,
mouseover: null,
}
},
beforeMount() {
Expand All @@ -71,6 +74,11 @@
this.showNotice();
eventBus.$on('toast.clear', this.close)
},
watch: {
mouseover: function (val) {
val ? this.timer.pause() : this.timer.resume();
}
},
methods: {
setupContainer() {
this.parentTop = document.querySelector('.notices.is-top');
Expand Down Expand Up @@ -103,7 +111,7 @@
},
close() {
clearTimeout(this.timer);
this.timer.stop();
clearTimeout(this.queueTimer);
this.isActive = false;
Expand All @@ -124,7 +132,7 @@
this.correctParent.insertAdjacentElement('afterbegin', this.$el);
this.isActive = true;
this.timer = setTimeout(this.close, this.duration)
this.timer = new Timer(this.close, this.duration);
},
whenClicked() {
Expand Down
29 changes: 28 additions & 1 deletion src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,31 @@ const removeElement = (el) => {
}
};

export {removeElement}
// custom timer with pause, resume ability
// thanks to: https://stackoverflow.com/a/3969760
class Timer {
constructor(callback, delay) {
this.startTime = Date.now();
this.callback = callback;
this.delay = delay;

this.timer = setTimeout(callback, delay);
}

pause() {
this.stop();
this.delay -= Date.now() - this.startTime;
}

resume() {
this.stop();
this.startTime = Date.now();
this.timer = setTimeout(this.callback, this.delay);
}

stop() {
clearTimeout(this.timer);
}
}

export {removeElement, Timer}

0 comments on commit 8d31285

Please sign in to comment.