2

The function below grabs a php page, then reloads it every 5 seconds. The only thing coming from that roomdata.php page is a string with a color name (blue, yellow, etc.). I wanted to be able to use that name in the function modifyLight(color), but it's not letting me. I don't know why, but no matter what I tried, it's not treating the variable data as a string, even if I clarify it as one.

Any help is appreciated, thanks!

$(function(){
  function loadData()
  {  
    var data = load('roomdata.php');
    modifyLight(data);
    setTimeout(loadData, 5000); // makes it reload every 5 sec
  }
  loadData(); // start the process...
});
Gosre
  • 61
  • 9
  • you are doing a recursive call there, put `setTimeout(loadData, 5000);` out of `loadData()` function – tttony Mar 16 '13 at 01:30
  • @ttony Thanks, but that doesn't really benefit towards my problem. The timeout worked either way. – Gosre Mar 16 '13 at 01:33
  • you should probably add `jquery` and `ajax` to your tags for this question. – Buttle Butkus Mar 16 '13 at 01:37
  • When you say it's not letting you, what does that mean exactly? Are you getting an error message? – ultranaut Mar 16 '13 at 01:37
  • I receive the data from the php page just fine. If I were to outprint document.write(load('roomdata.php')); it will display a color just fine (Blue, Yellow, ect), but I can't use that color as a string in the function modifyLight(color); – Gosre Mar 16 '13 at 01:41

1 Answers1

0

You are using async ajad calls. You need to configure your request to be sync.

 $.ajax(URL, {async : false});

In that way the execution of the next line will be done until the ajax request Is finished.

EDIT

Your function should be like this:

$(function(){
  function loadData() {
     $.post("roomdata.php", function(result) {
        modifyLight(result);
        setTiemout(function() { loadData(); }, 5000);
     }  
  }
  loadData(); // start the process...
});

The problem with the way you were doing it is that $.load(); only loads something with Ajax and put the content on $('#yourdiv'); It does not return anything. You need an ajax request with something in the "success" event. In the code I gave you, $.post makes an ajax request via post to roomdata.php and then, once the ajax is finnished, it executes the function function(result) { ... }

Cito
  • 1,659
  • 3
  • 22
  • 49
  • I receive the text just fine from the page and can easily display it with a printout or in a content area: $('#leftNav').load('roomdata.php'); but I can't use it in the function modifyLight(color) – Gosre Mar 16 '13 at 01:38
  • I think you will find useful [this post](http://stackoverflow.com/questions/1246137/ajax-jquery-load-versus-jquery-get). $.post sent the data with [POST method and $.get sent it with GET method](http://www.w3schools.com/tags/ref_httpmethods.asp). Also, if this is the solution, marked it as it... :) – Cito Mar 16 '13 at 03:41
  • Sorry, I dind't read well your comment. Can you please add the response you are receiving from that ajax request? – Cito Mar 16 '13 at 04:34
  • "Green" is what I'm receiving from the request.. Without the quotes. I want to be able to do the following: modifyLight(response goes here); – Gosre Mar 16 '13 at 14:16
  • The edition I made should work... in your`modifyLight()` function at the fist line of it make: `alert(TheParamYouAreReceiving);` and see what happens... – Cito Mar 17 '13 at 01:21