tags:

views:

75

answers:

2

Hi,

I am really confused by this few lines of code that should work in my opinion. Div is updating when using .html, but not when using custom script.

I have a couple files, index.php and test.php

index contains:

$(document).ready(function () {
    setInterval(function () {
        $(function () {
            $.ajax({
                url: 'test.php',
                dataType: 'json',
                cache: false,
                success: function (data) {
                    // custom script that displays parsed info goes here, doesn't work
                    $("#div").html(data); // works and updates
                }
            });
        });
    }, 10000);
});

test.php contains json string.

index.php displays, and updates the parsed info in a div just fine, however the script using the data populated by json doesn't refresh.

Any help will be greatly appreciated.

Thanks

A: 

First, check to make sure the request is being successfully completed. First double check your selector syntax matches the markup. '#div' is looking for any element with id="div". If that is correct make sure the request is getting processed. Try putting an alert or debug statement in the success function. If you don't get a popup use the firefox or chrome network monitor to see what is happening when the request is made.

mikerobi
+1  A: 

Using setInterval, calls the function at regular intervals of time, your function may or may-not execute completely , when you call the function using setTimeout, there is a chance for executing of code (completely). well In your question, I'm writing a self executing anonymous function with a function name as request and I'm gonna call it after every 10000 milli seconds

$(function () {
    (function request() {
        $.ajax({
            url: 'test.php',
            dataType: 'json',
            cache: false,
            success: function (data) {
                $("#selector").html(data); // works and updates
            }
        });
         //calling the anonymous function after 10000 milli seconds
        setTimeout(request, 10000);  second
    })(); //self Executing anonymous function
});

Just give it a try =)

Ninja Dude