Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:42+00:00 2026-06-12T01:38:42+00:00

I have several buttons on my page which are either start timer buttons, or

  • 0

I have several buttons on my page which are either “start timer” buttons, or “stop timer” buttons, which run startTimer and stopTimer functions respectively.

During the startTimer function, the button’s onclick changes from startTimer to stopTimer, and vice versa.

At the moment, once clicked, the button runs both functions one after the other perpetually.

I want it to run the function, then stop ready for the button to be clicked again and the opposite function to be run.

One approach I have been exploring is disabling the button while it’s running, having the functions check for an enabled button, running the function, then attempting to create an event to reenable it. It’s the last bit I’m struggling with.

function startTimer(project) {
var url = document.URL + '?timer=start&id=' + project;
var buttonid = 'button' + project;
var button =  document.getElementById(buttonid);
var stopText = 'stopTimer(' + project + ')';
if(button.disabled == false){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    var result = xmlhttp.responseText;

    if(result == 'ok'){
        button.disabled = true;
        button.setAttribute("class","btn btn-danger btn-mini");
        button.value = "Stop Timer";
        button.setAttribute("onclick", stopTimer(project));
    }
    else{
        alert("I couldn't start the timer.");
    }
}

}

function stopTimer(project) {
var url = document.URL + '?timer=stop&id=' + project;
var buttonid = 'button' + project;
var button =  document.getElementById(buttonid);
var startText = 'startTimer(' + project + ')';

if(button.disabled == false){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    var result = xmlhttp.responseText;

    if(result == 'ok'){
        button.disabled = true;
        button.setAttribute("class","btn btn-sucess btn-mini");
        button.value = "Start Timer";
        button.setAttribute("onclick", startTimer(project));
    }
    else{
        alert("I couldn't stop the timer.");
    }
}

HTML/PHP:

<?php if(in_array($project[0], $started)){ ?> 
                    <td>
                        <input type="button" class="btn btn-danger btn-mini" value="Stop Timer" onClick="stopTimer(<?php echo $project[0]; ?>)" id="button<?php echo $project[0]; ?>" />
                    </td>
                    <?php } else { ?>
                    <td>
                        <input type="button" class="btn btn-success btn-mini" value="Start Timer" onClick="startTimer(<?php echo $project[0]; ?>)" id="button<?php echo $project[0]; ?>" />
                    </td>
                    <?php } ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:38:43+00:00Added an answer on June 12, 2026 at 1:38 am

    I would recommend creating one function toggleTimer, and binding the button’s click to that. So, instead of futzing with the button’s click attribute, you would just need to adjust the url for your request accordingly.

    /copypasta

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one application which contains several buttons to home page clicking on
I have a web page on which there are several buttons and i want
I have an asp.net page. There is sidebar in which there are several buttons.
i have a page that contains several submit buttons however i only want one
I have a user control with several buttons, which need to take different actions
I have a form in which several buttons are added at runtime via a
I have several buttons on my page, but I'm not sure how to tell
I have a web page which has three <div> elements: Right_bar contains several layouts
I have a page which lists several messages, and each of these messages has
I have a ASPX Page that contains several user controls, which are loaded dynamically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.