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 8323517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:44:37+00:00 2026-06-08T23:44:37+00:00

My timer will stop when it reaches a certain number. Instead, I want it

  • 0

My timer will stop when it reaches a certain number. Instead, I want it to stop on a button click. How do I do that?
This is what my code looks like currently:

final TextView t1 = (TextView) findViewById(R.id.yourpay);

final Timer t =new Timer();
t.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                    public void run() {
                        money = (PPS+Reserve);
                        Reserve = (money);
                        t1.setText("$" + money); //Place your text data here
                        counter++;

                        //Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop. 
                        if(counter == HPDPS)
                            t.cancel(); 
                    }
            }); 
        }
    }, 1000, 1000);

If possible I would like it to stop on button click AND when counter reaches HPDPS.

  • 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-08T23:44:38+00:00Added an answer on June 8, 2026 at 11:44 pm

    Put in your button’s onClickListener():

    if (t != null)
        t.cancel();
    

    and remove the stopping condition from the timer.


    Code Example (updated):

    final TextView t1 = (TextView) findViewById(R.id.yourpay);
    
    final Timer t =new Timer();
    t.schedule(new TimerTask() {
    
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
    
                public void run() {
                    money = (PPS+Reserve);
                    Reserve = (money);
                    t1.setText("$" + money); //Place your text data here
    
                    // Removed the stopping condition/counter
    
                }
            }); 
        }
    }, 1000, 1000); // Do you really want to wait 1 second before executing the timer's code?  If not, change the 1st "1000" to a "0"
    
    
    final Button b = (Button) findViewById(R.id.my_button_id); // Replace with your button's id
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (t != null)
                t.cancel();
            }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a class that initializes a timer which will be used
Here is the functionality simply when click btnGo timer will stop and get it's
I have a timer class..This timer will be started from various portions of my
I have some code where the timer EventHandler has this void timer_Tick(object sender, EventArgs
Hey guys so I've got a working Timer, which will stop() when I call
I have a long running timer task. Its a batch process actually that will
thanks for taking the time to stop by my question. Below you will find
I have a timer which will be initialized on viewDidLoad, and be released on
I have a static timer class which will be called by ANY webpage to
Can we use SPSecurity.RunWithElevatedPrivileges in SharePoint timer jobs? Under whose identity will the timer

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.