ReadCheck.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view style="display: none;">用户行为监测...</view>
  3. </template>
  4. <script>
  5. export default {
  6. name: 'ReadCheck',
  7. props: {
  8. timeSec: Number
  9. },
  10. data() {
  11. return {
  12. goSec: 0,
  13. checkTimer: null,
  14. confirmTimer: null,
  15. dialogShow: false
  16. }
  17. },
  18. mounted() {
  19. console.log('定时', this.timeSec)
  20. this.initTimer()
  21. },
  22. destroyed() {
  23. this.clearTimer()
  24. },
  25. methods: {
  26. // 确认时间展示
  27. clearTimer() {
  28. if(this.checkTimer){
  29. clearInterval(this.checkTimer)
  30. clearTimeout(this.confirmTimer)
  31. }
  32. this.goSec = 0
  33. this.dialogShow = false
  34. },
  35. // 初始化定时器
  36. initTimer() {
  37. this.checkTimer = setInterval(() => {
  38. if (this.goSec >= this.timeSec && !this.dialogShow) {
  39. this.dialogShow = true
  40. this.showModal()
  41. // 30秒确认
  42. this.confirmTimer = setTimeout(()=>{
  43. clearInterval(this.checkTimer)
  44. this.$emit('break')
  45. }, 30000)
  46. }
  47. this.goSec += 1
  48. }, 1000 * 60)
  49. },
  50. showModal(){
  51. let that = this
  52. uni.showModal({
  53. title: '学习校验',
  54. content: `请点击确认按钮继续学习,请在30秒内点击确认按钮!`,
  55. success: function (res) {
  56. if (res.confirm) {
  57. that.clearTimer()
  58. } else if (res.cancel) {
  59. console.log('用户点击取消');
  60. that.dialogShow = false
  61. }
  62. }
  63. });
  64. }
  65. }
  66. }
  67. </script>
  68. <style scoped>
  69. </style>