0

I need to refresh a textarea element every few seconds. My solution involves calling the refresh method inside the controller inside the $("#element").ready with jquery ajax. I would expect that after the call has been made, the ready event would be refired, which unfortunately does not happen. Here is my javascript code :

$("#logTextArea").ready(function () {
    alert("ready!");
    setTimeout(reloadLogBox(), 5000);
});

function reloadLogBox() {
    $.ajax({
        url: '/json/AdminsStartUpdate/RefreshLogBox',
        type: "Post",
        success: function (response) {
            alert(response);
            $("#logTextArea").html = response;
        }
    });
}

and controller method (C# code) :

    [HttpPost]
    public JsonResult RefreshLogBox()
    {//breakpoint here
        //TODO : get log file contents
        string logContents = "Lorem ipsum dolor sit amet " + DateTime.Now.ToString("hh:mm:ss");

        return new JsonResult { Data = logContents };
    }

How can this be done correctly? I seached the internet a bit and found the on solution here Refire ready event after AJAX reload and in several other similar links, but I'm not certain on how to implement it in my particular case. Thanks!

Community
  • 1
  • 1
Alex Barac
  • 632
  • 4
  • 12

4 Answers4

2

I think you should use function pageload() in case of ajax call

Use below function may be it will help you, it's working for me

  setInterval(function() {
        reloadLogBox();
    }, 5000);
function reloadLogBox() {
    $.ajax({
        url: '/json/AdminsStartUpdate/RefreshLogBox',
        type: "Post",
        success: function (response) {
            alert(response);
            $("#logTextArea").html = response;
        }
    });
}

Something like this please refer below link

http://encosia.com/document-ready-and-pageload-are-not-the-same/

Yogesh Sharma
  • 2,017
  • 1
  • 15
  • 33
0

Try this

$("#logTextArea").ready(function () {
    alert("ready!");
    setinterval(reloadLogBox(), 5000);
});

Ready function is always called once. Textbox gets ready first time it loads. It won't load again. You are just making changes to it. So ready won't be called again. So instead of timeout use setinterval() method to call the function at regular intervals. Or you can use recursion in the ajax call when the call completes you can call settimeout again in the ajax call to repeat itself after some time.

0

ready event executes only once when HTML-Document is loaded and DOM is ready

Go through this link for more details.

Now, what you need is this.

    $(document).ready(
    function () {
        setInterval(function () {
            alert("refresh!");
        }, 500);
    });

OR

$("#logTextArea").ready(function () {
            setInterval(function () {
                alert("ready!");
            }, 500);
        });

DEMO

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
0

First you have to use setInterval instead of setTimeout since you need to fire this every few seconds. Try following code;

$("#logTextArea").ready(function () {
    alert("ready!");
    setInterval(function () { reloadLogBox() }, 5000);
});

function reloadLogBox() {
    $.ajax({
        url: '/home/RefreshLogBox',
        type: "Post",
        success: function (response) {
            alert(response);
            $("#logTextArea").html(response);
        }
    });
}

There $("#logTextArea").html = response; should be $("#logTextArea").html(response);. Thanks!

Saranga
  • 3,178
  • 1
  • 18
  • 26