1

I'm trying to work out if values have changed through javascript. Essentially doing a check on a textbox to see if the value changes.

However, when I put the value in to a variable it comes back as undefined. Clearly i'm doing something silly wrong but my tired head can't work it out.

Here's what i'm doing:

<script>
    var stableCount;
    $(document).ready(function () {
        $('.div-updated').hide();
        $("#responsecontainer").load("CyberTable.aspx");
        stableCount = $(".rowcount").val();
        var refreshId = setInterval(function () {
            alert(stableCount + " - " + $(".rowcount").val())
            $("#responsecontainer").load('CyberTable.aspx?t' + Math.random());
        }, 3000);
        $.ajaxSetup({ cache: false });
    });
</script>

The stableCount in the alert comes back as undefined, but the rowcount.val() comes back with the number

EDIT: Please just don't downvote with no reason. As far as i'm aware i've followed all rules to post this and attempted to do it myself.

4
  • 1
    The load is an asynchronous operation - it launches a load but you only know it completes when any callback returns success, not when the next instruction runs Commented Nov 25, 2013 at 13:22
  • I would set "stableCount = $(".rowcount").val() || 0;" this way, the stableCount would get a value if it is undefined. Commented Nov 25, 2013 at 13:23
  • 2
    Post your heml code. Tell me .rowcount value whats return? Commented Nov 25, 2013 at 13:34
  • See my more detailed answer below... Commented Nov 25, 2013 at 13:37

3 Answers 3

1

This answer clarifies what I said in my comments. Once you launch a load operation it runs as a separate process and you only know it has loaded the data when a complete or error callback is called. The code needs modifying to have a callback to the load operation, and to start the timer events from within the callback

<script>
$(document).ready(function () {
    $('.div-updated').hide();
    $("#responsecontainer").load("CyberTable.aspx",
       // callback function here....
       function() {
          var stableCount = $(".rowcount").val();
          var refreshId = setInterval(function () {
              alert(stableCount + " - " + $(".rowcount").val())
             $("#responsecontainer").load('CyberTable.aspx?t' + Math.random());
          }, 3000);
    });

    $.ajaxSetup({ cache: false });
});

Sign up to request clarification or add additional context in comments.

1 Comment

That makes perfect sense. Thank you for taking the time to explain why it does what it did.
1

the .load is asynchronous. Because of this, $(".rowcount").val() is undefined at the time you assign the stableCount value, but available 3 seconds later in the interval function.

4 Comments

I don't undestand, how does the asynchronousness of the load affect the inputs value? Atleast the code he presented does not support that.
lol I got there first but as a comment :-) @Esa - see my comment
So is there not a way to do this at all?
Simply check the load succeeds through a callback and start your timer events from the callback
1

use stableCount variable in scope of document object. Because stableCount is not global here

<script>
$(document).ready(function () {
    $('.div-updated').hide();
    $("#responsecontainer").load("CyberTable.aspx");
    var stableCount = $(".rowcount").val();
    var refreshId = setInterval(function () {
        alert(stableCount + " - " + $(".rowcount").val())
        $("#responsecontainer").load('CyberTable.aspx?t' + Math.random());
    }, 3000);
    $.ajaxSetup({ cache: false });
});

1 Comment

Correct but not an answer to the question :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.