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

  • Home
  • SEARCH
  • 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 6903011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:52:17+00:00 2026-05-27T07:52:17+00:00

I am trying to use AJAX polling with jQuery to update a span element

  • 0

I am trying to use AJAX polling with jQuery to update a span element on a razor MasterPage in ASP.NET MVC3. The page uses the jQuery Mobile 1.0 framework that adorns simple view changes (like navigating from /home to /about) with some sort of “transition” animation.

This is the Javascript code that does the polling, while the “unreadBubble” span is located in the body – both are defined in the MasterPage!

<script type="text/javascript">
$(document).bind("pageinit", function poll() {
    setTimeout(function () {
        $.ajax({ url: "/Notification/GetUnreadNotificationsCount", 
                    dataType: "json",
                    success: function (data) {
                    $('#unreadBubble').text(data.UnreadCount);
                    poll();
                } 
            });
    }, 1000);
});

So, imagine I have a HomeController and a NotificationController that both use the MasterPage and provide an Index view. The AJAX polling works on both views and updates the span every second as expected. As soon as I navigate from one view to another though, the span gets re-initialized with its default value from the MasterPage (empty) and doesn’t update anymore. Interestingly the async GetUnreadNotificationsCount method is still called on the NotificationsController repeatedly – the span just doesn’t update. I also tried to alert the span tag in JS and it wasn’t null or something.

According to the documentation, jQuery Mobile also loads new pages with AJAX to insert this fancy “SWOOSH” transition animation. This seems to somehow disturb the JS/DOM initialization.

Do you have any idea how to resolve this? Should I bind to another event or can I somehow force the span tag to update?

Solution: It was a caching problem! The following did the trick:
Add class=”command-no-cache” to your page div add the following JavaScript to the MasterPage:

$(":jqmData(role=page)").live('pagehide', function (event, ui) {
if ($(this).children("div[data-role*='content']").is(".command-no-cache"))
    $(this).remove();

});

  • 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-27T07:52:18+00:00Added an answer on May 27, 2026 at 7:52 am

    I would use the pagebeforeshow to actually bind the event, and pagehide to remove the event.

    Did you try that instead of initializing only once in the pageinit event?

    UPDATE: some code for example,

     <script type="text/javascript">
     var timer = null;
     $(":jqmData(role=page)").bind("pagebeforeshow", function() {
         timer = setTimeout(function() {
                   $.ajax({ url: "/Notification/GetUnreadNotificationsCount", 
                      dataType: "json",
                      success: function (data) {
                         $('#unreadBubble').text(data.UnreadCount);
                      } 
                   });
                 }, 1000);
     }); 
     $(":jqmData(role=page)").bind("pagehide", function() {
         if (timer != null){
              clearTimeout(timer);
              timer = null;
         }
     });
    </script>
    

    Also corrected some other “”mistypes” along the way, have a look and compare to your code!

    Hope this helps

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

Sidebar

Related Questions

I'm trying to use jquery.Ajax to post data to an ASP.NET MVC2 action method
I'm trying to use Ajax Toolkit in ASP.NET page to display a Calendar Extender
I'm trying to use AJAX via jquery with ASP.NET MVC, but I'm not able
I am running into a problem trying to use AJAX and jQuery with ASP.NET
I am trying to use jQuery's ajax functionality to update data from a web
I am trying to make use of jQueryUI AJAX tabs in my ASP.Net Webforms
I'm trying to use a google visualisation, the column chart, inside an asp.net AJAX
I'm trying to use some AJAX and ASP.Net together to enable me to run
I'm experiencing a strange problem while trying to use the Ajax.BeginForm method of ASP.NET
Im trying to use AJAX to update a partial on a page. I following

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.