0

Trying to refresh a div every 60 seconds, using the following javascript:

<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load').fadeOut('slow').load('index.html #load').fadeIn("slow");
}, 60000);
</script>  

Here is the corresponding div:

<div class = "well" id="load">
  <center><h3><i class="icon-bar-chart"></i> Mt.Gox</h3></center>
  <center><h3>&#579;1 = <?php echo $usd; ?> USD</h3></center>
<hr>
  <center><h3><i class="icon-bar-chart"></i> BTC-e</h3></center>
  <center><h3>&#579;1 = <?php echo $usde; ?> USD</h3></center>
</div>

The div is currently not refreshing at all.

Thanks for any help!

6
  • Any errors on the console? Commented Oct 8, 2013 at 16:15
  • You're probably missing the references to jQuery files. Try adding this <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>. Commented Oct 8, 2013 at 16:16
  • 1
    <center></center> is deprecate. use css text-align: center; instead Commented Oct 8, 2013 at 16:17
  • What data is being loaded from the URL? Does it have an elemnt with ID load? Commented Oct 8, 2013 at 16:18
  • jsfiddle will be good to debug.. or share code with all included js. Commented Oct 8, 2013 at 16:18

3 Answers 3

3

Ok that's because you are using the script incorrectly., You need to use callbacks as below

    <script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#load').fadeOut('slow',$(this).load('index.html #load', 
       function(){
          $(this).fadeIn("slow");
       })
    )
    }, 60000);
    </script>  
Sign up to request clarification or add additional context in comments.

Comments

1

Edit: I misunderstood the code a bit so ignore my original comment below!


Try to remove the #load in the url. It's perfectly valid according to the jQuery API, but I think in this case it can cause reduncancy.

That is, make it like this instead:

$('#load').fadeOut('slow').load('index.html').fadeIn("slow");

When you call .load() on an element it will load into that element, if you also specify the same element in the Url to load - I think it can cause a problem.

11 Comments

Nope, it's really just fine.
Right, but in your example, there is no #load section in the loaded document. Of course it will fail when you say "load a section that does not exist". The #load in the function parameter isn't saying where to load the data - its a selector, telling jQuery which part of the loaded document should actually be used.
Ah, of course - I got a bit confused since they are both called #load :) Sorry for the confusion. Adding a second element with the same ID can not be a good idea however since they are supposed to be unique, maybe it could still be a reason for the error?
That's not what's going to happen. load() will pull the contents of the #load element from the loaded data, and replace the contents of the current document's #load element with that.
@MichaelHazelwood that's because, you forgot to include jquery library
|
-1

Please see this link: Origin null is not allowed by Access-Control-Allow-Origin

Also that issue, I think that you shouldn't load html data via file:/// URL. Why don't you load a div in the same file. Forexample:

$('#load').fadeOut('slow').load($('#test').css('display', 'inline')).fadeIn("slow");

and the div tag is like this:

div id="test" style="display:none;"

2 Comments

Thanks, with style=display:none nothing is displayed at all -- not even the data available at page load. Secondly I still get the TypeError: $ is not a function error, despite the fact that jquery-1.9.1.min.js is included and all the other javascript items on my page are working fine.
If you want it is displayed at page load, so remove style=display:none and change the javascript code like this one: $('#load').fadeOut('slow').load('#test').fadeIn("slow"); Remember that div tag that has id "test" is in the same file. Hope this help.

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.