A perpetual JavaScript countdown that constantly loops with a delayed response

I'm currently working on an online auction bidding project that involves users placing bids before time runs out. I found a solution on this link.

My goal is to run the timer loop continuously. When the timer hits 0, I want the bid button to be disabled and then enabled when the timer starts again. How can I achieve this? Below is the code I referenced from the provided link which only runs twice.

function timer(startFrom, delay, intervalDelay, runTimes, notFirstRun){
    if(typeof runTimes == "undefined") runTimes = 1;
    if(runTimes-- < 0) return;
    setTimeout(function(){
        var ctn = startFrom;
        var timer1 = window.setInterval(function(){
            document.getElementById('out1').innerHTML = ctn--;
document.getElementById('out2').innerHTML = "You can Place BID!";
            if(ctn <= 0){
gotit();
                clearInterval(timer1);
                timer(startFrom, delay, intervalDelay, runTimes, true);
            }

        }, intervalDelay);
    }, notFirstRun?delay*1000:0);
}
window.onload=function() {
    timer(10, 5, 1000, 2);
}    
function gotit()
{
document.getElementById('out2').innerHTML = "Bidding closed!";
}
<html>
<body>
    <div>Biddind closes in <span id="out1"></span> seconds!</div>
<div id="out2">You can Place BID </div>
<form>
<input type="text" name="amount"/>
<input type="submit" id="btnct" value="Place Bid"/>
</form>

</body>
</html>

Answer №1

To enable or disable the button with id "btnct", simply insert document.getElementById("btnct").disabled = false/true in the appropriate location.

function timer(startFrom, delay, intervalDelay, runTimes, notFirstRun){
    if(typeof runTimes == "undefined") runTimes = 1;
    if(runTimes-- < 0) return;
    setTimeout(function(){
        document.getElementById("btnct").disabled = false;
        var ctn = startFrom;
        var timer1 = window.setInterval(function(){
            document.getElementById('out1').innerHTML = ctn--;
document.getElementById('out2').innerHTML = "You can Place BID!";
            if(ctn <= 0){
                document.getElementById("btnct").disabled = true;
gotit();
                clearInterval(timer1);
                timer(startFrom, delay, intervalDelay, runTimes, true);
            }

        }, intervalDelay);
    }, notFirstRun?delay*1000:0);
}
window.onload=function() {
    timer(10, 5, 1000, 2);
}    
function gotit()
{
document.getElementById('out2').innerHTML = "Bidding closed!";
}
<html>
<body>
    <div>Biddind closes in <span id="out1"></span> seconds!</div>
<div id="out2">You can Place BID </div>
<form>
<input type="text" name="amount"/>
<input type="submit" id="btnct" value="Place Bid"/>
</form>

</body>
</html>

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

When the route is modified, the image fetched from the JSON disappears

When using a div tag to display JSON values on an image, I noticed that the values disappear when navigating to other pages and then returning to the home page. This issue is occurring as I develop with Angular.js NumberJson.get().then(function (resul ...

What is the best way to arrange an array of identical objects based on a specific sub property?

Here's an array of items to work with: myArray = [ { someDate: "2018-01-11T00:00:00", name: "John Smith", level: 5000 }, { someDate: "2017-12-18T00:00:00", name: "Jane Doe", level: 1000 }, { ...

When using Chart JS, is there a way to create a line graph without including any labels at all?

Currently, I am facing a challenge with my Chart JS line graph. It needs to pull data from a backend and display it on a webpage. However, the chart has close to 1000 points to plot, making it impossible for me to provide labels for each point on both the ...

What is the best way to create a dropdown menu that smoothly slides in from the bottom of the screen?

Is it possible to create dropdown menus in the navigation bar that slide in from the bottom with a smooth transition effect, similar to this example: Although I am working on building my own template, so my code may differ from the original theme. Here is ...

Ensuring input validity and blocking changes if not compliant with AngularJS patterns

I am looking to create an input field that only accepts the values 1, 2, or 3. To achieve this, I am working on a directive that will prevent any changes to the model if it doesn't match these specific values. For example, if the value is 1 and I tr ...

Limit the number of AJAX requests running simultaneously using jQuery

Looking to optimize a series of AJAX events with a limit of 5 simultaneous requests and queueing up the rest. Consider the scenario below: $("div.server").each(function() { $.ajax(....); }); With potentially 100 divs containing the class server, o ...

Incorporating dynamic JavaScript animations with immersive 360-degree images

I've been exploring ways to replicate a solution similar to this one: Sample During my research, I came across some lightweight jQuery plugins that can enable 360 degree image rotation. However, there are still a few things I haven't figured out ...

Angular 4 - Issues with route configurations

My Angular application is running smoothly on localhost:4200 using ng serve. The node server can be found at localhost:3000. After running ng build, a bundle file is generated and properly served from localhost:3000 thanks to the line app.use(express.sta ...

Clicking the button will remove any previously applied CSS style using jQuery

Is there a way to remove the background color that was applied to a previously clicked button when clicking on another button? The current code successfully changes the background color of the clicked button upon hover, but it doesn't reset or remove ...

The execution of the enzyme wrapper.update() function results in the removal of the value prop from the ref input

Take a look at this Code Sandbox that showcases a test scenario related to the issue being discussed. The test in the mentioned Code Sandbox is failing as outlined in the question: https://codesandbox.io/s/react-jest-and-enzyme-testing-c7vng The goal here ...

What is the reason behind JavaScript's restriction on using double comparison operators?

What is the reason behind javaScript not supporting double comparison? For example, in 64 < str.charCodeAt(i) && str.charCodeAt(i)<=77, why is it not possible to simplify it to 64 < str.charCodeAt(i)<=77? ...

Best method for transferring dynamic data from PHP to JavaScript in Moodle

I have a modal popup within my Moodle filter that requires certain variables to be defined in my PHP script, which I am struggling to set correctly in JS. For example, "MyVariable": define(['jquery', 'core/modal_factory'], function($, ...

How can I eliminate text using regular expressions?

Greetings, I am seeking assistance with content removal using regular expressions. Below is the regular expression code I have written: reg = reg.replace(/\|.*?(\|)/g, ''); Input: One-1|two-2|Three-3|Four-4|Five-5 Six-6|Seven-7|Eig ...

Creating a visual representation from an array of strings to produce a

My goal is to organize a list of server names into a map using a specific logic. For instance, with names like: "temp-a-name1", "temp-a-name2", "temp-b-name1", "temp-b-name2" They would be mapped as: { a: [ "temp-a-name1", "temp-a-name2" ] ...

Utilize a variable within a regular expression

Can the variable label be used inside a regex like this? const label = 'test' If I have the regex: { name: /test/i } Is it possible to use the variable label inside the regex, in the following way? { name: `/${label}/i` } What do you think? ...

Order of callback execution in jQuery's ready function

When two JavaScript functions on a page need to be called once the document load is complete, is there a possibility that one function could be executed before the other, or will it always be the same order? For example, using jQuery with the following co ...

Setting up a personalized configuration entry in environment.js

I am currently working with EmberJS version 2.4.2 and I have a specific requirement to handle custom configuration entries using an environment.js file. var ENV = { APP: { myKey: "defaultValue" } }; While everything works perfectly in development ...

Exploring the playful side of DPI evaluation

I have a function that adjusts the size of an image based on the window.devicePixelRatio. Now, I am looking to properly test it. const wrapper = mount(myComponent, { propsData: { size: { height: 500, width: 500, }, f ...

Is there a way to authenticate the sort code in order to retrieve the corresponding bank name?

Does anyone know of a library or component that can verify sort codes and return the corresponding bank name? For example, I currently use card-validator to check if a card is valid and identify its type (Visa or Mastercard). Now I am looking for a simil ...

How can the center element be aligned without recursively centering all children?

How can I center all children within a particular element without affecting their children? <Col style={{ textAlign: "center" }}> <Space>...</Space> <div>...</div> </Col> In this scenario, I am utilizing t ...