Disrupt my AngularJS code with a countdown interruption

I am feeling a bit disappointed with my countdown function:

/* countDowner */
var countDown = 5;
function countDowner() {
    if (countDown < 0) {
        $("#warning").fadeOut(2000);
        var countDown = 0;
        return; // exit
    } else {
        $scope.countDown_text = countDown; // update scope
        setTimeout(countDowner, 1000); // repeat the loop
        countDown--; // decrease by 1
    }
}
$scope.countDown_text = countDown;
countDowner();

I added it to an AngularJS controller and it's causing issues with my code :/ There is an error, but it seems to be related to another part of the code. Everything works fine when I remove the countdown function. Can anyone help me figure out what's wrong with my countdown?

Answer №1

The issue with the code not functioning correctly is due to the fact that you have redefined the variable countDown.

// initial value
var countDown = 5;
// later ...
var countDown = 0;

For a solution, refer to this fully functional example http://jsfiddle.net/ahbtez18/

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

Adding elements to an array

router.get("/api/cart", auth, async (req, res) => { try { const user = await User.findById(req.user._id); items = []; await user.cartProducts.forEach(async (product) => { var item = await Item.findById(product._id); ...

Choosing2 - incorporate a style to a distinct choice

Let's talk about a select element I have: <select id="mySelect"> <option>Volvo</option> <option value="Cat" class="red">Cat</option> <option value="Dog" class="r ...

Navigating through a intricate JSON dataset

Hello, I apologize for any language barriers as French is my native tongue. I am reaching out to seek assistance with navigating a complex JSON object using a loop in Javascript, particularly with JOINTJS. I am struggling to access the information I need ...

How can a "Loading" message be displayed while external JavaScript is loading?

I have mastered the art of using JS or jQuery to showcase a "Loading" message during the loading process. Currently, I am working on a sizeable web application that relies on various JS dependencies, and I am seeking a way to exhibit a "loading" message wh ...

How to toggle a div's visibility in AngularJS with a click事件

I am developing a filter that requires me to create 3 popups using the ng-repeat directive in Angular. Each popup should have the same class name but a different id. When a button is clicked, I want one popup to show while the rest hide. I currently have ...

AngularJS is experiencing delays when processing subsequent $http delete requests, leaving them stuck in a

I am currently working on a project where I have a table displaying a list of objects, with each object having multiple child objects associated with it. The technologies I am using include Angular 1.2.20, Express 4.6.1, and Node 0.10.25. In the table, the ...

Challenges with JavaScript Regular Expressions

Looking to extract a specific Number enclosed within square brackets, like so: Identify the 0 within actionFields[actionFields][0][data[Report][action]] I've been working on this but keep getting a null result. var match, matchRegEx = /^\(?&bs ...

The inner nested ng-repeat section is not properly binding to the scope variable and appears to be commented out

In my code, there is a nested ng-repeat set up. The 'boards' variable in the scope is an array that contains another array called 'tasks', which also consists of arrays. In the innermost ng-repeat, I am attempting to bind to task.conten ...

Using ng-bind-html within a ng-repeat loop

Currently, I am working on developing a custom autosuggest feature where a web service is queried with a search term to retrieve a list of suggestions that are then displayed. My main obstacle lies in trying to highlight the matching term within the sugges ...

Extract the "_id" value from the array in the v-for loop and then store and reuse it in the data object // using vue-cli

Currently, I am iterating through [postArray] to retrieve various posts, each of which has its own unique "_id". My goal is to utilize this "_id" to add likes to the corresponding post. This is the v-for loop I am using: <div class="posts" v- ...

How can I prevent the Bootstrap popover (dragged) from moving unexpectedly after being dragged?

Is there a way to make a draggable popover without it jumping around? Currently, when I drag it, the position is based on its location in the DOM due to top:0 and left:0 positioning with transitions. Any solutions? HTML <div class="row"> <div c ...

Slide in the Toggle Effect to Your Navigation Menu

Seeking help with enhancing my jQuery dropdown menu to include a slide down toggle effect similar to this example: http://jsfiddle.net/LaSsr/188/. Any assistance in applying this slide effect to the following JSfiddle would be greatly appreciated. Thank yo ...

Display a singular list of items within one HTML tag for a custom field in Meteor.user, instead of each item having its own separate tag

There is a custom user field that is filled by the user upon clicking a button with an ID of an item from another collection. However, when returned, all saved items are displayed in one HTML tag instead of each saved item having its own tag. The result en ...

retrieving specific values from a row in an HTML table and converting them into a JSON array

function extractRowData(rowId) { const row = [...document.querySelectorAll("#stockinboundedittable tr")].find(tr => tr.id === rowId); const rowData = Object.fromEntries( [...row.querySelectorAll("input")].slice(1).map(inp => [inp.id.replace(/ ...

Starting an Angularjs CSS3 animation with JavaScript

I am currently exploring how to create a CSS3 animation in Angular.js. Before starting the animation, I need to establish the initial CSS properties using Javascript. Is there a way to kickstart an animation with Javascript and then smoothly transition ...

Creating an Array in AngularJS with ng-model and submitting it with jQuery: A comprehensive guide

I am struggling to submit an array of values using jQuery and AngularJS. Whenever I click the submit button, only the first array value is being retrieved. How can I get all array values using ng-model? Here is a link to all my code: https://jsfiddle.net/r ...

Is the confirmation window not showing up on the active tab in Chrome Extension? It only seems to appear on the popup.html tab

I have created a Chrome extension for an e-gym that is designed to prompt a confirm window after every hour of using the Chrome browser. The issue I am facing is that the confirm window only appears on popup.html. I want to activate my extension so that it ...

Utilizing Vue.js for Tabs in Laravel 5.8

I am encountering an issue in Laravel while attempting to set up a Vue instance for tabs. Currently, only Tab 1 and Tab 2 are displayed without any content, and the tabs themselves are not clickable links. Could this problem be related to how I am calling ...

Is there a way to access and troubleshoot the complete source code within .vue files?

I've been struggling for hours trying to understand why I'm unable to view the full source of my .vue files in the Chrome debugger. When I click on webpack://, I can see the files listed there like they are in my project tree, but when I try to o ...

Why does the content-type header in the request contain additional information beyond just the MIME type?

There have been instances where the content-type header of a request (such as one made by Firefox) includes not just information about the MIME type, but also details on encoding. For example, when sending JSON via AJAX, instead of application/json (whic ...