laravel - Vue 倒计时在 IOS 浏览器上不起作用
问题描述
使用 Laravel 在 Vue 中进行倒计时
嗨,我在 Vue.js 和 Laravel 中创建了一个倒计时,它在所有浏览器中都能完美运行,但在 IOS 浏览器上却不行。
在这里你可以看到我的代码:
刀片文件上的 Vue 组件
<countdown-button
until="{{ $active->ends_at }}"
url='{{ route('dashboard.active.show', $active->uuid) }}' />
正如你所看到的,它是一个使用 props 的普通组件,我必须不使用“:”,因为如果我添加它,我会得到一个错误,它说它必须是 String 的数字实例,当使用 props 时。
这是我的 Vue.js 组件
<template>
<div class="countdown__container">
<div v-if="finished">{{ endsAt }}</div>
<div v-else class="countdown__date--container">
<div class="countdown__date--item2">
<span>{{ remaining.days }} days</span>
<span>{{ remaining.hours }} hours </span>
<span>{{ remaining.minutes }} minutes </span>
<span>{{ remaining.seconds }} seconds </span>
</div>
</div>
<div :show="butt" class="lower-box">
<a :href="url" class="theme-btn style-one">
<i class="fas fa-user"></i>PLAY NOW
</a>
</div>
</div>
</template>
<script>
import moment from 'moment';
export default {
props: {
url: String,
until: {
type: String,
required: true,
},
endsAt: { default: 'Game finished!' },
},
data () {
return {
now: new Date(),
butt: true,
};
},
created () {
this.refreshEverySecond();
},
computed: {
finished () {
return this.remaining.total <= 0;
},
remaining () {
let remaining = moment.duration(Date.parse(this.until) - this.now);
if (remaining <= 0) this.$emit('finished');
return {
total: remaining,
years: this.pad(remaining.years(),2),
months: this.pad(remaining.months(),2),
days: this.pad(remaining.days(),2),
hours: this.pad(remaining.hours(),2),
minutes: this.pad(remaining.minutes(),2),
seconds: this.pad(remaining.seconds(),2)
};
}
},
methods: {
pad(num, size) {
var s = "000000000" + num;
return s.substr(s.length-size);
},
refreshEverySecond () {
let interval = setInterval(() => this.now = new Date(), 1000);
this.$on('finished', () => {
clearInterval(interval);
this.butt = false;
});
}
}
}
</script>
<style lang="scss">
.countdown__date--item2 {
font-size: 16px;
color: orange;
font-weight: bold;
}
</style>
这个组件只有两个 props,它们是 url 和 endsAt。它只显示倒计时。
它在桌面浏览器和 Android 上 100% 运行
但在 IOS 浏览器上显示如下:
解决方案
diff()
我认为你应该在 computed 中使用 moment remaining
:
remaining () {
let until = moment().from(this.until);
let now = moment().from(this.now);
let remaining = moment().duration(until.diff(now));
if (remaining <= 0) this.$emit('finished');
return {
total: remaining,
years: this.pad(remaining.years(),2),
months: this.pad(remaining.months(),2),
days: this.pad(remaining.days(),2),
hours: this.pad(remaining.hours(),2),
minutes: this.pad(remaining.minutes(),2),
seconds: this.pad(remaining.seconds(),2)
};
}
推荐阅读
- javascript - 道具在“render”函数中工作时在“render”函数上方显示空值?
- javascript - react.js 文档中的 HOC
- reactjs - 即使我正在清理我的 useEffect,如何避免我的反应应用程序中的内存泄漏警告?
- kotlin - 如果变量不为空,则分配给变量的更简洁方法
- python-3.x - 如何使用python将一串像素转换为图像?
- git - 如何在不删除文件、数字和重复的情况下存储“git pull”的输出,以便在文本文件中列出新文件?
- c# - 使方法参数不合格
- scala - 如何在 udf 中使用数据框并在 spark scala 中解析数据
- mysql - 对话 SQL 结构(群聊)
- ghci - GHCi 中的 PrimOps