Stopping the timer with clearInterval isn't functioning as expected

I posted my work online here:

Instructions on how to reproduce:

  • Click on a green pin on the map to select a station
  • Fill in the fields for name and last name
  • Sign on the canvas

  • A timer should start counting down from 20 minutes

  • If you click on the "cancel" button, the timer should be reset
  • If you then resign on the canvas, the timer should restart from 20 minutes

The timer functionality is located in the file /js/timer.js. I am facing an issue where even though I use clearInterval(this.setTimer) and sessionStorage.clear() in my cancel function, the timer is not resetting properly. Debugging shows that this.setTimer increases by 1 every time I cancel, although it should be resetting to 10.

Am I missing something? The desired behavior is for the timer to reset to 20 minutes when the "cancel" button is clicked.

Below is my cancel function:

cancel = () => {
    this.isPlaying = false;
    clearInterval(this.setTimer);
    sessionStorage.clear();
    console.log(this.setTimer);
    canvas.clear();
    this.timer.innerHTML = 'Your reservation has been canceled.'
    document.getElementById('reservation-content').classList.remove('hidden');
    document.getElementById('cancel-btn').className = 'hidden';
    document.getElementById('validation-btn').setAttribute('disabled', null)
}

Answer №1

this.delta

retains the previous value after subtracting 1000.

Upon clearing the interval, remember to reassign the this.delta value.

this.delta = (this.endDate - this.now);

Insert the above line after

clearInterval(this.setTimer);

within the cancel method.

I trust this response adequately addresses your query.

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

Only enable the last day of each month on the React Material UI date picker, all other dates in the year should be disabled

I've been struggling to find a solution that allows users to only choose the last day of each month in a year, disabling all other days. After searching for answers using the Material UI date picker, I have not been successful. If anyone can guide me ...

Slow CSS :hover animations in the most recent release of Chrome

I recently upgraded my browser from chromium version 67 to the latest Chrome version 79. However, I noticed that after the upgrade, the CSS transitions on my website have become very laggy and unresponsive. Surprisingly, everything works perfectly fine on ...

Instead of automatically playing, Youtube videos either remain idle or display suggested videos

I am trying to play a specific moment of an embedded Youtube video using some javascript code. At the specified time, I execute the following code: document.getElementById("video").src= "https://www.youtube.com/embed/...?autoplay=1&start=212"; where ...

What sets $vm.user apart from $vm.$data.user in Vuejs?

When you need to retrieve component data, there are two ways to do so: $vm.user and $vm.$data.user. Both methods achieve the same result in terms of setting and retrieving data. However, the question arises as to why there are two separate ways to access ...

How can we maintain line breaks in Textfields when using React-Admin and Material UI?

Currently, I am working with React-Admin and encountered an issue where my database field contains line breaks (\n). However, when I try to display it on a page using the following code: <TextField source="extra_details" /> The line breaks are ...

Is Ember CLI experiencing issues due to the latest Ember Data update?

Greetings! I am a beginner with Ember and recently encountered some warnings after upgrading to the latest version of Ember Data: Update: I have two identical versions of my app, one built without ember-cli and the other with ember cli. Both applications ...

I need help figuring out how to send a POST/GET request from AJAX to a custom module controller in Odoo 10, but I'm running into issues

I have implemented a custom module in Odoo 10 with a simple controller. Everything works smoothly when accessing http://127.0.0.1:8069/cmodule/cmodule through the browser, displaying the expected return string. However, I encountered an issue when attempt ...

Enhance the sequence of tweens by triggering them sequentially in response to rapid UI events

I am currently working on my three.js app setup and I am facing a challenge with creating a smooth transition between two models. The desired effect is to have the first model quickly scale down to zero, followed by the second model scaling up from zero t ...

What's the process for creating a Java object in PHP and utilizing it in JavaScript?

I am looking to create an object on a PHP page and send it as a response through an AJAX call to be used as a JavaScript object on the response page. This type of object is what I need to generate and pass along. var areaChartData = { labels ...

In JavaScript, when using the fetch function with JSON, it is possible to skip the

Here's an example of fetching review data from within a 'for loop': fetch('https://api.yotpo.com/products/xx-apikey-xx/{{product.id}}/bottomline') In this case, some products may not have reviews and will return a 404 response. Th ...

javascript how to retrieve the custom attribute value of an HTML style

I am dealing with an HTML element that has some inline styles as well as a custom CSS property. I am able to access every style attribute except for my custom style property. Here is the code I am working with: <img id="myimg" class="ImgClass" style=" ...

Using Ajax with the submitHandler function in jQuery Validation Plugin

Currently, I'm working with the jQuery validation plugin in conjunction with ASP.NET MVC. I am trying to utilize $.Ajax() to submit a form, but I'm encountering an issue where the entire section of code I have written seems to be getting skipped ...

Validating Angular UI without requiring an input field (validating an expression)

Currently, I am utilizing ui-validate utilities available at https://github.com/angular-ui/ui-validate The issue I am facing involves validating an expression on a form without an input field. To illustrate, consider the following object: $scope.item = ...

Extracting Data from JSON Using Vue.js

I am facing an issue with extracting data from a JSON file using Vue.js. Below is the HTML and JSON data along with the script. Any help would be appreciated. <!DOCTYPE html> <html> <head> <title>Vu ...

Error: the function is not defined, therefore the variable is being passed

I'm attempting to pass a variable in the URL in order to automatically check a radio button on a different page depending on which link is clicked. However, I keep encountering an "Uncaught ReferenceError: radio_onload is not defined" error. Could th ...

Toggle button in React following a list iteration

Upon receiving data from an API call to Google Books, I want to hide the description paragraphs and implement a toggle button using the "hidden" CSS class from Tailwind CSS. Currently, I am just logging the elements on the "view description" button and uns ...

The module named "jquery" has not been loaded in this context: _. Please use require() to load it

As I work on migrating my Javascript files to Typescript, I encountered an issue when trying to use the transpiled javascript file in an HTML page. The error message I received is as follows: https://requirejs.org/docs/errors.html#notloaded at makeError (r ...

Focusing on the initial element following CSS prioritization

Check out this codepen link: http://codepen.io/muji/pen/XpEYzO I am looking to target the first element after it has been sorted using a CSS "order" property and apply another CSS property to it. Is there a way to use jQuery or any other method to identi ...

Encountering Zip File Corruption Issue post MongoDB Download

My goal is to attach a zip file to an object and request the zip file when the object is called from the client. I was successful in uploading the zip file using the following code: const upload = multer({ fileFilter(req, file, cb){ if (!file. ...

What are the steps for implementing middleware that relies on a socket connection?

Using express.io, I am currently working on creating a middleware that necessitates a connection to a remote server through two sockets. However, I have encountered an issue. var net = require('net'); module.exports = function (host, port) { ...