PDA

View Full Version : how to "setTimeout" a function?



smansakra
01-15-2009, 07:04 AM
hello....
how to "setTimeout" a function?
i can make setTimeout if the functionis simple, but i'm rather confusing about below
for example like the function below

function adding(id,textku){
var el = document.getElementById(id);
el.innerHTML+=textku;
setTimeout("adding(id,textku)",1000);
}

please pay attention to the bold one*
this "setTimeout("adding(id,textku)",1000);" does not work, i tried to change to this " setTimeout("adding("+id+","+textku+")",1000); " but it also doenst work either.

but it works well -> setTimeout("adding()",1000);

can anyone help?

smansakra
01-15-2009, 07:34 AM
i'm trying to change to this

function adding(id,textku){
setTimeout(function(){document.getElementById(id).innerHTML+=textku;},1000);
}

but unfortunately, this function will just display 1 time after 1 second,
what i want here is this function display 1 second, then it displays again after 1 second , and it will display again after 1 secon and so on...

hum, how?

jscheuer1
01-15-2009, 07:50 AM
function adding(id,textku){
setInterval(function(){document.getElementById(id).innerHTML+=textku;},1000);
}

smansakra
01-15-2009, 07:57 AM
aha! that works, so i should use setInterval, not setTimeout,
thanks you,

hi, wait, what about is my function is so long,
for example like this one:


function myNewComm(what,mode,opt){
var myNewAjax = createAjaxObj();
if(!myNewAjax) return false;
var url_myNew = "processing/newComments"+what+".php?mode="+mode;
url_myNew +="&r="+Math.random();
myNewAjax.open("GET",url_myNew);
myNewAjax.onreadystatechange=function(){
if(myNewAjax.readyState==4 && myNewAjax.status==200){
if(mode=='check'){
if(myNewAjax.responseText.indexOf('no new comment')!=-1) return 0;
else return parseInt(myNewAjax.responseText);
}
else if(mode=='display') displayMyNewComm(what,myNewAjax.responseText);
}
}
myNewAjax.send(null);
if(opt==true) setTimeout("myNewComm("+what+","+mode+","+opt+")",15000);
}

hum, i'm quite confusing

finally, i can do by doing this:

function myNewComm(what,mode,opt){
if(opt==true){setInterval(function(){
var myNewAjax = createAjaxObj();
if(!myNewAjax) return false;
var url_myNew = "processing/newComments"+what+".php?mode="+mode;
url_myNew +="&r="+Math.random();
myNewAjax.open("GET",url_myNew);
myNewAjax.onreadystatechange=function(){
if(myNewAjax.readyState==4 && myNewAjax.status==200){
if(mode=='check'){
if(myNewAjax.responseText.indexOf('no new comment')!=-1) return 0;
else return parseInt(myNewAjax.responseText);
}
else if(mode=='display') displayMyNewComm(what,myNewAjax.responseText);
}
}
myNewAjax.send(null);
},15000);}
}

that's would be better

HAH ! I HATE THIS !
my above function has 3 arguments what,mode and opt
based on my code above , if i use this
if(opt==true){setInterval(function(){ , the function wil be just execute if opt == true , what about if my opt==false? should i make a function again like this:
if(opt==false){setInterval(function(){
BLA
BLA
},15000);

:hard thinking

what i want is, if opt==true the function will be repeated, but if opt==false the function just excuted 1 time.

HUM?

Twey
01-15-2009, 12:03 PM
Please don't post so many times in a row. There is an 'edit' button at the bottom of your posts that you can use to update them if you think of something new to add.

Firstly, you don't want to create that massive function every time. It would be far preferable just to create a small function:
function myNewComm(what, mode, opt) {
var myNewAjax = createAjaxObj(),
url_myNew;

if (!myNewAjax) return false;

url_myNew = "processing/newComments" + what + ".php"
+ "?mode=" + mode
+ "&r=" + Math.random();

myNewAjax.open("GET", url_myNew);

myNewAjax.onreadystatechange = function() {
if (myNewAjax.readyState === 4 && myNewAjax.status === 200)
if (mode === "check")
if (myNewAjax.responseText.indexOf("no new comment") > -1)
return 0;
else return parseInt(myNewAjax.responseText);
else if (mode === "display")
displayMyNewComm(what, myNewAjax.responseText);
}

myNewAjax.send(null);

(opt ? setInterval : setTimeout)(function() { myNewComm(what, mode, opt); }, 15000);
}Stylistically speaking, this function does far too much. You should break it down into several smaller functions. Also, I think that terming any of this 'AJAX' is rather misleading, since there doesn't appear to be any XML involved at all, and your function names are rather silly and quite uninformative.

smansakra
01-16-2009, 07:09 AM
ups sorry for that, after i posted i tried it again and again , then i posted the result of my trial.
so it look so many times posting,

hum, i will see your code:)


(opt ? setInterval : setTimeout)(function() { myNewComm(what, mode, opt); }, 15000);

aha, i never think about this one, it looks something good