YourNameHere Posted September 5, 2009 Share Posted September 5, 2009 I have an Ajax call that works...once But I need it to re-call itself ever second. and it needs to start on page load. I am using jquery for the Ajax. I tried a setTimeOut(chatupdate(), 1000); but this didn't seem to work, it broke all my other scripts. I am sure this is just a syntax error. please explain what I am doing wrong. Here is my code: function chatupdate(){ $('#chatbox').load('chatbox.php'); setTimeout(chatupdate(), 1000) } Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 I had the same prolem a while ago too. You need to put the function in setTimeout inside quotes. function chatupdate(){ $('#chatbox').load('chatbox.php'); setTimeout("chatupdate()", 1000) } Try this. Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 5, 2009 Author Share Posted September 5, 2009 Well that makes the rest of the scripts work but the Ajax does not work now. :-\ Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 ajax isnt workin? . What do you mean? The chat isnt updatin. Try lookin at the error console. What does it say?Or can you post the ajax code here? Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 5, 2009 Author Share Posted September 5, 2009 yes the error console says there is an error at line 1 char 1 on the chat page. No clue what that means. the code was posted already edit: sorry it says "Object expected" Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 5, 2009 Author Share Posted September 5, 2009 function chatupdate(){ $('#chatbox').load('chatbox.php'); setTimeout("chatupdate();", 1000); } chatupdate(); With this code, everything works, Ajax and other scripts. Except the update every second. Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 I think youre usin a javascript library. jquery or somethin. I dont use it, so I onno much about the code :-\. If youre tryin to refresh the contents of div with id chatbox from the php code chatbox.php I can post the code to do that. Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 Okay, "Object Expected"s the error message that you get when the functions expecting an object and youre passin a string or somethng else to it. Try adding this line to your code. Donno if itll help. function chatupdate(){ chatbox=document.getElementById('chatbox'); $(chatbox).load('chatbox.php'); setTimeout("chatupdate();", 1000); } chatupdate(); Or you must have some code somewhere which is set to accept the object. Can you post the whole javascript code here? Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 5, 2009 Author Share Posted September 5, 2009 I am using a framework, and your are absolutly correct with how it works. The ajax call is not the issue here, it works properly. It is all wrapped up in a function that I need to call every 'x' seconds. That is what I need to do. Imagine that the code I proveded was done without a framwork. It would work the same. We'll call it, ajax(); I need ajax(); to fire every 1 second until the the end of time. I just found setInterval();... tried it, no dice. Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 Ohkay try this function chatupdate(){ $(chatbox).load('chatbox.php'); chatupdate=function(){ chatupdate(); } setTimeout("chatupdate;", 1000); } Its some kind of a closure. Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 5, 2009 Author Share Posted September 5, 2009 :-\ nope I need sleep, thanks for your help. Will try again in the morning. It would seem that setTimeout() is deprisiated. but i dont know. Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 Wrapping the function inside a quote should solve your problem. If youre not sure try this code. javascript: function check(cnt){ alert("Timd Out "+cnt); parseInt(cnt); if(cnt<5){cnt++;setTimeout("check("+cnt+")",1000);}} check(0); It should execute 5 times every 1 second. So theres something wrong with the function youre tryin to execute. Quote Link to comment Share on other sites More sharing options...
mrwutang Posted September 5, 2009 Share Posted September 5, 2009 cool, good night homes. . Sometimes all you need is a good sleep. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 5, 2009 Share Posted September 5, 2009 you are looking for setInterval() not timeout and you do not need it inside of the function itself function x(){} setInterval("x()", 1000); should do it Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 5, 2009 Share Posted September 5, 2009 Actually quotes in this situation shouldnt be used. function x(){ console.log('xxx'); } setInterval(x, 1000); Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 5, 2009 Share Posted September 5, 2009 You can actually do some pretty cool stuff with it. if you wanted the function to execute before the first interval call and still have a reference to it that you can pass around, do this (var y = function(){ console.log('xxx'); })(); setInterval(y, 1000); Quote Link to comment Share on other sites More sharing options...
YourNameHere Posted September 6, 2009 Author Share Posted September 6, 2009 Sleep was all I needed. Working code: chatUpdate(); function chatUpdate(){ $.ajax({ url : 'chatbox.php', cache : false, type : 'get', success : function(data){ $('#chatbox').html(data); setTimeout(chatUpdate, 1000 ); } }); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.