Evaluating time stamps consistently

I am attempting to pass an unlockTime prop to a component1 in order for it to be rendered after the specified time has elapsed.

How can I make Vue continuously check if the current time is greater than the unlockTime (

Date.now() > this.unlockTime ? true : false
)?

Main.vue:

<template>
<component1
  :unlockTime="unlockTime">
</component1>
</template>

<script>
  computed: {
    unlockTime() {
      return Date.now() + (5 * 60 * 1000)
    }
  }
</script>

Component1.vue

<template>
  <div v-if="unlock">
    Some Content Here
  </div>
</template>

<script>
  props: ["unlockTime"]

  data(){
    return{
      unlock: Date.now() > this.unlockTime ? true : false
    }
  }
</script>

Answer №1

Here's a straightforward approach:

  1. Utilizes the setInterval method to fetch the current date and time

  2. Employs a watcher/computed technique to update the data property=isLock by comparing the current date and time with the unlock date and time.

Vue.component('child', {
  template: `<div>
              <p v-show="isLock"><span>Waiting for Unlock...{{unlock}} - {{currentDateTime}}</span></p>
              <p v-show="computedIsLock"><i>computed lock:{{computedIsLock}}</i></p>
             </div>`,
  props: ['unlock'],
  created: function () {
    this.intervalMgr = setInterval(()=>{
      this.currentDateTime = new Date()
    }, 500)
    this.isLock = this.currentDateTime < this.unlock
  },
  data(){
    return {
      isLock: true,
      currentDateTime: new Date(),
      intervalMgr: null
    }
  },
  computed: {
    computedIsLock: function () {
      return this.currentDateTime < this.unlock
    }
  },
  watch: {
    currentDateTime: function (newVal) {
      this.isLock = newVal < this.unlock
    }
  },
  beforeDestroy: function () {
    clearInterval(this.intervalMgr)
  }
})

app = new Vue({
  el: "#app",
  data: {
    unlockTime: new Date()
  },
  created: function () {
    this.addTime()
  },
  methods: {
    addTime: function () {
      this.unlockTime = new Date()
      this.unlockTime.setSeconds(this.unlockTime.getSeconds() + 5)
    }
  }
})
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a1d7d4c4e1938f948f9097">[email protected]</a>/dist/vue.js"></script>
<div id="app">
    <button @click="addTime()">Add Time</button>
    <child :unlock="unlockTime"></child>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine whether the current page was reached by pressing the back button

Can we determine if the current page was loaded via a back button press? Here is the scenario: index.html (contains a link to page1 in the menu) page1.html (loads content from ajax with a link to page2) page2.html (user presses the BACK button) page1.h ...

JavaScript code that dynamically updates values on a canvas

I have a particle animation created using JavaScript and Canvas. My goal is to adjust the canvas drawing values, specifically radMax and radMin. Here is the code snippet for reference: https://jsfiddle.net/u3wwxg58/ Currently, when I invoke function f(), ...

Ways to trigger a function upon clicking the button (X) within a VueJS search input field

When Button (X) is clicked in the search input, I need to trigger an action using Vue. What should happen when the user clicks on "x" in the search input? new Vue({ el: "#app", data: { msg: 'the input is cleaned', info:'&a ...

Submit data via POST method on the modified URL

I need assistance with modifying the data of a URL and making a POST request to it. The URL in question is as follows: http://domanin.com/search-result/?start=06%2F08%2F2017&end=06%2F09%2F2017&room_num_search=1&adult_number=1&children_num= ...

What is the best way to combine PHP and HTML within a JavaScript script?

I'm facing a challenge with integrating dynamically added elements and a populated drop down on my form. Is there a way to combine the two seamlessly? Below is the code snippet I'm using for dynamically adding and removing elements: $(docu ...

Tips for effectively validating with ParsleyJS remote validator?

I've been trying to solve a problem for hours now, but I can't seem to figure out how to retrieve a value in php and pass it back to js. Here is the input field in question: <input type="text" class="form-control input-lg" name="nr_ref" id=" ...

What is the mechanism behind the jQuery ready function that enables its first parameter to transform into a jQuery object?

While browsing today, I came across this interesting code snippet: jQuery(document).ready(function($$){ console.log($$); }); It seems that $$ is recognized as the jQuery object itself. Typically, to achieve this, we would need to pass the jQuery varia ...

Using special symbols in HTML5 data attributes

Is it feasible to locate all DOM elements using jQuery with wildcard characters in the attribute name? Take into consideration the following HTML code: <input id="val1" type="text" data-validate-required data-validate-minlength ...

Find the most accurate color name corresponding to a hexadecimal color code

When given a hex-value, I aim to find the closest matching color name. For instance, if the hex-color is #f00, the corresponding color name is red. '#ff0000' => 'red' '#000000' => 'black' '#ffff00' = ...

Inconsistency found in Ajax DataTable data updates

Incorporating a DataTable ajax feature, I pass values to the controller. Here is a simplified version of the code: $(function() { $("#tableDiv").hide(); $("#submitDateFilters").on("click", function() { displayData(); $("#tableDiv").show(); ...

Occasionally, Knockout associates an element with [object HTMLDivElement] through binding

My UI application is utilizing requireJs and knockout js. In the main html file (index.html), there is a div element as follows: <html style="height:100%" lang="en"> <head> <title>Main</title> <script data-main="my ...

Using jQuery Plugin for Date Operations

I am in search of a jQuery tool that can assist me with date manipulations, such as: 1) Adding a specific number of days to a date and obtaining a new Date. 2) Determining the week of the year for a given date. 3) Calculating the number of days between two ...

Tips for ensuring a module.export function completes its request before assigning the returned value to a variable in another JavaScript file

Within my Node.js application, I am utilizing two JS files. One of these files, latlng.js, contains the following code: async function getPlaceDetails(input) { var locationUrl = 'https://www.google.com'; request(locationUrl, (error, respo ...

Issues with integrating the jsPDF package into a JavaScript project

I'm struggling to solve this issue. I've been attempting to create a program that can download a pdf from a webpage using the jsPDF npm module. After downloading it, I tried importing it in two different ways: Using the node.js require statemen ...

How can I show a dynamic popover with specific content when a "Copy" action is triggered?

I am attempting to copy a value to the clipboard: /** * If the browser does not support direct clipboard manipulation, use an alternate method * This is used in the copyTextToClipboard function. * * Function found at: https://stackoverflow.com/a/3 ...

JavaScript failing to accurately measure the length

Currently experiencing a Javascript issue where the length of an element is not displayed correctly when using .length, even though it shows up in Chrome console. Here is what it looks like in Chrome console <html xmlns="http://www.w3.o ...

Implementing useState to toggle the checked value of a checkbox in React

When passing a list of checkbox fields with attributes to my component, I want to update the checked attribute based on user selection. However, all I have managed to do so far is double the check value in an array. How can I modify this specific key with ...

What could be causing the absence of pagination links on my website?

While attempting to adjust the width of the images, I noticed that the navigation pager below (pages 1,2,3) unexpectedly disappeared. As a CSS beginner, I'm unsure why this happened and how to resolve it. The code for the navigation is still present, ...

JavaScript asynchronous problem

When developing a simple Javascript function to validate user input for City and State, it encounters an issue with asynchronous behavior. The function checks if the user has entered values in the specified input fields and then proceeds to geocode the l ...

Combine text in vue.js within a form

I'm looking to merge the texts I've created in vue into a single text Example: text field 1 = Code text field 2 = Area text field 3 = NumberCode Then I have one text field UniqueNumber, which is the value of this text field a combination of ...