Jump to content

[SOLVED] Recursive function not working


YourNameHere

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 );
         } });
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.