0

I have a java servlet which responds with a number in text/plain format. What I want to do is have another page run a script which polls this URL and triggers an alert window when the number there has changed from the first time it was loaded. I have little to no experience with javascript and so far have been unable to follow any of the long-polling tutorials I have found.

Basically, I need to start the poll on page load, remember what the first value retrieved was, and then trigger an alert window once the value changes.

This is what I have so far, however I believe I need to parse the data somehow. The URL returns a text/plain page with a number on it. That's it.

var initial = null;

    function checkForMsg() {
        $.ajax({
            type: "GET",
            url: URL_GOES_HERE,
            dataType : "text",
            async: true,
            cache: false,

            success: function(data) {
                if (initial == null) {
                    initial = data
                }
                else if (initial != data) {
                    alert(data);
                }

                setTimeout('checkForMsg()', 12000);

            },
            error: function() {
                setTimeout('checkForMsg()', 12000);
            }
        })
    }

    $(document).ready(function(){
        checkForMsg();
    });
agilesynapse
  • 41
  • 1
  • 5
  • Keep in mind that code speaks louder then words, it's aleways a good idea to add sample of your code to your questions. It will draw more attention and allow users to give you samples to try on your side – legrandviking Jul 30 '13 at 18:45
  • 1
    I appended what I have so far. I seem to be in need of some sort of parse on the data that has been retrieved from the URL. – agilesynapse Jul 30 '13 at 19:26

0 Answers0