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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:07:08+00:00 2026-06-12T05:07:08+00:00

I have the following javascript code that is trying to call a server side

  • 0

I have the following javascript code that is trying to call a server side function:

    function server_GetStats(nodeID) {
        var result = PageMethods.getStats(nodeID);
        return result;
    }

    setInterval(function () {
        newVal = parseInt(server_GetStats(1089)) + parseInt(server_GetStats(1090));
        rate = (newVal - val) / (pollTime / updateCounterTime);
    }, pollTime);

And this is the server side function that is being called:

    [WebMethod]
    public static int getStats(object nodeID)
    {
        int stat= 0;
        SqlConnection conn = new SqlConnection();
        string connStr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
        conn.ConnectionString = connStr;
        conn.Open();

        string sql = "SELECT stat FROM NODE_PROPERTIES WHERE NodeID = " + Int32.Parse(nodeID.ToString());
        SqlCommand cmd = new SqlCommand(sql, conn);

        stat = Int32.Parse((cmd.ExecuteScalar().ToString()));
        conn.Close();
        return stat;
    }

I’ve added asp:ScriptManager to the aspx page as well. Can’t for the life of me figure out why I’m getting NaN. I checked that the SQL statement is OK too. Could someone shed some light on what I’m doing wrong?

Answer:

As was suggested I added a callback function. Ended up looking something like this:

    setInterval(function () {
        newVal = 0;
        server_GetStats(1089, 1090);
    }, pollTime);

    function server_GetStats(nodeID) {
        PageMethods.getStats(nodeID, OnGetStatsSuccess, OnGetStatsFailure);
    }

    function OnGetStatsSuccess(result) {
        newVal = parseInt(result);
        rate = (newVal - val) / (pollTime / updateCounterTime);
    }

    function OnGetStatsFailure(result) {
        //do something when your server-side function fails to return the desired value
    }

Code-behind stayed the same.

  • 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-12T05:07:10+00:00Added an answer on June 12, 2026 at 5:07 am

    When you are calling a PageMethod, it is getting called asynchronously. That means, when you call server_GetStats(1089), it is returning from that function before the PageMethod even completes. To get your code to work, you’ll need to define a callback for your PageMethod call. Something along these lines:

    var values = 0;
    
    function myCallback(result) {
        values += parseInt(result);
    
        // Maybe call a function here that notifies/changes the UI.
    }
    
    function server_GetStats(nodeID) {
        PageMethods.getStats(nodeID, myCallback);
    }
    

    Reference: http://www.geekzilla.co.uk/View30F417D1-8E5B-4C03-99EB-379F167F26B6.htm

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

Sidebar

Related Questions

I have the following Javascript code that works perfectly: $(document).ready(function() { $(#Select1).setDefault(); $(#Select2).setDefault(); $(#Select3).setDefault();
I have the following jQuery ajax call setup: function Testing() { var result =
I have the following JavaScript/jQuery code that starts the listener that highlights the DOM
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have the following JavaScript code: var x = ['CFMG','JMFMG','CPMAF']; var y = $.param({'test':x});
I have the following JavaScript code: oCoord = {x: null, y: null}; var aStack
I have the following Javascript code add_num = { f: function(html, num) { alert(this.page);
I have the following Javascript code (using jQuery): floatUpAndDown(); function floatUpAndDown() { $(#bird).animate({top: '+=30px'},
So I have the following JavaScript code: <script type=text/javascript> function clearRadioButtons() { document.getElementById(radiobutton1).checked=; //etc
I have the following code below that I'm trying to set a background color.

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.