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
https://forums.phpfreaks.com/topic/173203-solved-recursive-function-not-working/
Share on other sites

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.  :D

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?

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.

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.