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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:45:23+00:00 2026-05-24T21:45:23+00:00

i have this timer in c#: public class HomeController : Controller { public int

  • 0

i have this timer in c#:

public class HomeController : Controller
{
    public int count = 0;
    public ActionResult Index()
    {
        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Elapsed += new ElapsedEventHandler(onTimer);
        timer.Start();
        return View();
    }
    public void onTimer(Object source, ElapsedEventArgs e)
    {
        count++;
    }  
    [HttpPost]
    public JsonResult CheckStatus()
    {
        return Json(count);
    }

and this is my view:

<script type="text/javascript">
$(function () {
    // poll every 5 seconds
    setInterval('checkStatus()', 3000);
});

function checkStatus() {
    $.ajax({
        url: 'Home/CheckStatus',
        type: 'POST',
        dataType: 'json',
        success: function (xhr_data) {
            document.getElementById("test").innerText = xhr_data;
        }
    });
}

Why does it keep show me (when running) only 0 ? why doesn’t it show me: 0,1,2 etc.. ?

  • 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-05-24T21:45:24+00:00Added an answer on May 24, 2026 at 9:45 pm

    A new instance of the controller will be created each time you make a request. Thus your repeated AJAX calls just get 0, since that’s what the count value is initialized to.

    There will likely be other controllers (with timers running) floating around until the GC runs to collect them, but they will have no effect on the new controller that is actually processing each individual request.

    I don’t know what problem you’re trying to solve with this code, but have you investigated the setTimeout and setInterval javascript methods? They provide client-side timer services, and might be well-suited for your task.

    If you need persistent server-side data, then there are number of options, including:

    • Session state
    • Application state
    • ViewState
    • The Cache

    Which, if any, are right for you depends on the actual problem you’re trying to solve.

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

Sidebar

Related Questions

I have this class ('Scheduler.as'): package { import flash.events.TimerEvent; import flash.utils.Timer; public class Scheduler
This is my scenario. I have internal class with a standard int property. public
I have a class that creates and uses a System.Threading.Timer, something like this: using
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I have found this example on StackOverflow: var people = new List<Person> { new
I have a service class in my Android app which uses a timer. The
Well basically I have this script that takes a long time to execute and
I have this recorded in SQL Server: 1- startTime (datetime): 5/2/2009 08:30 (brazilian time
I have been building enterprise software for the last 10 years. In this time
I have had to do this several times, usually when trying to find in

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.