Refreshing a value in the view at regular intervals using a filter in Vue

Trying to create a countdown timer with vue, but the view is not updating. Here are my app.js and index.html:

var nowDate = new Date;
var nextNewYearsEve = new Date(nowDate.getFullYear(), 11, 31, 23, 59, 59, 59);
var timeLeftToNewYearsEve = nextNewYearsEve - nowDate;
var app = new Vue({
el: '#app',
data: {
timeLeftToNewYearsEve: timeLeftToNewYearsEve
},
filters: {
date: function (val) {

var timeLeftMonth = new Date(val).getMonth();
var timeLeftDay = new Date(val).getDay();
var timeLeftHour = new Date(val).getHours();
var timeLeftMinutes = new Date(val).getMinutes();
var timeLeftSeconds = new Date(val).getSeconds();

return timeLeftMonth + ' month  ' + timeLeftDay + ' day  ' + timeLeftHour + ' hour  ' + timeLeftMinutes + ' minute  ' + timeLeftSeconds + ' second';
}
},
methods: {
update: function () {
setInterval(function () {
timeLeftToNewYearsEve = nextNewYearsEve - new Date();
console.log(timeLeftToNewYearsEve);
}, 1000);
}
}
});

app.update();
<html>
<head>
</head>
<body>
<div id="app">
{{timeLeftToNewYearsEve | date}}
</div>
<script src="https://unpkg.com/vue"></script>
<script src="resource/app.js"></script>
</body>
</html>

Any idea why the value in the view is not updating?

Answer №1

Utilize the this keyword to access Vue values in your code.

setInterval(() => {
  this.daysLeftToChristmas = nextChristmas - new Date();
  console.log(daysLeftToChristmas);
}, 1000);

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

React Redux Loading progress bar for seamless navigation within React Router

Currently, I am working on adding a loading bar similar to the one used by Github. My goal is to have it start loading when a user clicks on another page and finish once the page has fully loaded. In order to achieve this, I am utilizing material-ui and t ...

Add the contents of `$scope.data` into a JSON object using

I am trying to update a JSON file with data entered in form elements, but I seem to be encountering some issues. <html ng-app="example"> <div ng-controller="ListController"> <form name="myform" ng-submit="addThis()"> ...

Encountering an issue while trying to launch an Angular application on localhost. Vendor.js resource failed to load

Whenever I compile the code, it runs successfully. However, when I try to serve the application using 'node --max-old-space-size=8192', even though it compiles without any errors, when I open the app in a browser, it returns an error saying "Cann ...

Gathering the presently unfinished observables within a superior-level rxjs observable

As an illustration, let's consider a scenario where I have a timer that emits every 5 seconds and lasts for 10 seconds. By using the scan operator, I can create an observable that includes an array of all the inner observables emitted up until now: c ...

Exploring the intricacies of React Router parameters

I have been facing some issues with my routing setup. Specifically, I have a static route called list and a dynamic route called user/:id. The problem arises when navigating between these two pages. (1) Whenever I navigate to the list route from the user/: ...

The Kendo Date Picker is failing to update properly when the k-ng-model is modified

I am facing an issue with my code involving two date pickers and one dropdown list. I want the date pickers to change based on the selected item from the dropdown list. Here is the relevant portion of my code: $scope.toolbarOptions = { i ...

Guide on dynamically setting environment properties in Vue.js during deployment when using it alongside a Spring Boot application

Vue Js and Springboot have been packaged together in a Jar file, with the Vue files being included as static content within the jar. Currently, UI specific environment variable properties are configured at build time using npm run build. However, I would ...

The Json object's size increases exponentially with each subsequent return

There seems to be a strange issue with the PHP file that is causing the JSON objects to duplicate. The first time I run the script, I receive 5 rows, which matches the number of rows in the table. However, on the second run, I get double the results. Wha ...

Issue arises after injecting HTML element into the DOM due to memory constraints and display inconsistencies

Upon executing the following script in Firefox... var d = $("<div class='test'></div>"); d.hide(); $("body").prepend(d); d.show(); ...and examining the HTML, the inserted element will have the style attribute: style="display: blo ...

`validate.js verifying the elements within an array`

One of the challenges I'm facing in my JavaScript project is dealing with objects that have two array properties included. As part of my development process, I've decided to utilize the resources provided by the validate.js library. To illustrat ...

Displaying tooltips dynamically for newly added elements all sharing a common class in React

I'm facing an issue with the primereact tooltip component from . Everything seems to be working fine except for the target property. When I have multiple elements on a page with the same class, the tooltip works as expected. However, when I add a new ...

When the JSON array is converted into a string, it appears as undefined

Below is a snippet of my service.spec.ts file: service.spec.ts it('should mock the http requests', inject([Service, MockBackend], (service, mockBackend) => { let result:any; mockBackend.connections.subscribe((connection) => { ...

What is the best way to integrate Halfmoon's JS from npm into my current code using Gulp?

I am eager to incorporate the Halfmoon framework into a personal project and have successfully downloaded it through npm. To utilize the example JavaScript provided on this page (found at ), I need to import the library using a require statement. var halfm ...

Leverage PHP variables within AJAX requests

I am currently working on implementing a specific functionality on the page "videos.php" (please note that this is all contained within a PHP echo statement): First, when a user clicks .star_' . $pvid_ID . ', it triggers the submission of a vid ...

Efficiently sending a cookie with an Axios POST Request

My request is not receiving a cookie even after trying various solutions like withCredentials. I have pasted the most recent code here, can anyone spot what might be missing? var cookie_for_data = "token=test"; var host = "http://localh ...

Implementing MUI createTheme within Next.js

After switching from material-UI version 4 to version 5.5.0 in my project, I encountered issues with createTheme. The colors and settings from the palette are not being applied as expected. Current versions: next: 11.0.0 react: 17.0.2 mui : 5.5.0 theme. ...

How can I use Vue to close the side Navbar by targeting the body tag and clicking anywhere on the screen?

I am looking to make a change in the Navbar position by moving it right: -500px (to close the navbar) when clicking anywhere on the window. In HTML and JS, I can achieve this by targeting the body tag. How can I accomplish this in Vue? I already have funct ...

Tips on incorporating a changing variable into a JavaScript Shader

I have successfully created a primitive sphere using THREE.SphereGeometry and applied a displacement shader to give it a bumpy effect. My goal now is to animate the scale of the bumps based on input volume from the microphone. Despite my efforts, I am stru ...

Manipulating div positions using JQuery and CSS

Whenever I hover over the #HoverMe div, a hidden #hidden div appears, and when I unhover it, the hidden div disappears again. The #hidden div contains a sub-div that functions like a dropdown list. The #hidden div is styled with position: absolute;. How ca ...

Flot seems to be having difficulty uploading JSON files

I am relatively new to working with json and flot, but have been tasked with creating a chart. Can someone please help me troubleshoot why my code is not functioning as expected? $.getJSON('chart.json', function(graphData){ alert(graphData); ...