1

I am developing a small web application. I have pretty much finished the server side code, but am struggling with the client side javascript.

I have an html file that is updated every second. This file is fully static and I use it only for log purposes.

The idea is to do pretty much the same thing, but with an auto update done on the client side.

For that I want to use database polling using ajax.

I have implemented a first version here, that simply prints the same html file and is supposed to reload it every second.

The thing is that is doesn't update, even though I don't see any error in my javascript console client side. The file is found, as it loads correctly and I don't see any reason why it wouldn't update my div.

Here is my code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- Using Google CDN -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script>
        (function worker() {
          $.ajax({
            url : 'http://jlengrand.pythonanywhere.com/static/leader.html',
            success: function(data) {
              $('.text').html(data);
            },
            complete: function() {
              setTimeout(worker, 1000);
            }
          });
        })();
    </script>
</head>
<body>
    <div class="text">
        To be loaded <br />
    </div>
</body>
</html>

I tried locally (but with a smaller file) and it worked fine.

I know that reloading the whole file is not clever, but I want to build incrementally as this is the first time I develop in JavaScript.

What am I missing?

1

4 Answers 4

7

Error: useless setTimeout call (missing quotes around argument?)

You run worker inside setTimeout. You need to remove () or you need to put it in '':

setTimeout(worker, 1000);

or

setTimeout('worker()', 1000);

*Edit: To prevent caching, change url to:

 url : 'http://jlengrand.pythonanywhere.com/static/leader.html?rand='+Math.random(),
Sign up to request clarification or add additional context in comments.

9 Comments

setTimeout(worker, 1000); is what i have on my webpage. I added () while trying to debug
@jlengrand, Ok then update your html file at your server too.
It is. jlengrand.pythonanywhere.com/static/index.html :). Or am I missing something?
It works for me after removing (). try installing tamper data on firefox and see it yourself.
@jlengrand or select something and you can see your selection go away each 3 sec. Mean div updated.
|
3

It seems like you are not calling the worker function on document ready. Therefore the loop does not get initiated. Bear in mind that if you are calling a file located on a different domain you will face cross domain issues.

Check this code, it may solve your problem:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <!-- Using Google CDN -->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
   <script type="text/javascript">
    $(document).ready(function(){
       worker();
    });

    function worker(){
        $.ajax({
            url: 'http://jlengrand.pythonanywhere.com/static/leader.html',
            success: function(data) {
                console.log(data);  
                $('.text').html(data);
            },
            complete: function() {
                  setTimeout(worker(), 1000);
            }
        });
    }
   </script>
 </head>
 <body>
   <div class="text">
       To be loaded <br />
   </div>
</body>
</html>

1 Comment

This is a big problem but not his problem, because it is an ajax call and there is little chance to complete before completion of whole page.
2

Every thing is correct but doc ready is missing:

$(function(){
    (function worker() {
      $.ajax({
        url : 'http://jlengrand.pythonanywhere.com/static/leader.html',
        success: function(data) {
          $('.text').html(data);
        },
        complete: function() {
          setTimeout(function(){ // use this way
             worker();
          }, 1000);
        }
      });
    })();
});

Comments

0

Alternate solution to reload 1 second after the last load using Deferred

function getData() {
    $.ajax({
        url: 'http://jlengrand.pythonanywhere.com/static/leader.html',
        success: function (data) {
            $('.text').html(data);
        },
        complete: function () {}
    });
}

function showDiv() {
    var dfd = $.Deferred();
    $('.text').fadeTo(1000, 1, dfd.resolve);
    return dfd.promise();
}

function startme() {
    $.when(getData(), showDiv())
        .then(function (ajaxResult) {
        // ‘ajaxResult’ is the server’s response
            startme();
    });
};
startme();

Comments

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.