Results 1 to 6 of 6

Thread: how to "setTimeout" a function?

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Arrow how to "setTimeout" a function?

    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?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    function adding(id,textku){
    setInterval(function(){document.getElementById(id).innerHTML+=textku;},1000);
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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:
    Code:
    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?
    Last edited by Twey; 01-15-2009 at 10:47 AM.
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default ups sorry for that

    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
    Last edited by smansakra; 01-16-2009 at 07:25 AM.
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •