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...
});
5
  • you are doing a recursive call there, put setTimeout(loadData, 5000); out of loadData() function Commented Mar 16, 2013 at 1:30
  • @ttony Thanks, but that doesn't really benefit towards my problem. The timeout worked either way. Commented Mar 16, 2013 at 1:33
  • you should probably add jquery and ajax to your tags for this question. Commented Mar 16, 2013 at 1:37
  • When you say it's not letting you, what does that mean exactly? Are you getting an error message? Commented Mar 16, 2013 at 1: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); Commented Mar 16, 2013 at 1:41

1 Answer 1

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) { ... }

Sign up to request clarification or add additional context in comments.

5 Comments

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)
I think you will find useful this post. $.post sent the data with POST method and $.get sent it with GET method. Also, if this is the solution, marked it as it... :)
Sorry, I dind't read well your comment. Can you please add the response you are receiving from that ajax request?
"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);
The edition I made should work... in yourmodifyLight() function at the fist line of it make: alert(TheParamYouAreReceiving); and see what happens...

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.