Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [HELP] JavaScript Auto Message

    Could someone figure out why this is not working, I've looked at it for hours now, its probably because im tired but I just cant figure it out.

    Okay basically there is an API I am using and that all works fine, to be knowledge, which is "Models.chat.sendChat" Now I wanted to do an auto post every minute (possibly 5 minutes) of a random post. This is what I have got;

    Code:
    function speakAuto()
    {
    var responsesAuto = new Array (
    "text0", 
    "text1", 
    "text2", 
    "text3", 
    "text4", 
    "text5", 
    "text6", 
    "text7", 
    "text8", 
    "text9", 
    "text10", 
    "text11", 
    "text12"
    );
    var ind = Math.floor(Math.random() * 13);
    message = "/em"+responcesAuto[ind]+"";
    Models.chat.sendChat(message);
    }
    
    setTimeout(function(){speakAuto();}, 60000);
    What is it that I have done wrong? It says that the syntax is correct and everything, but it doesn't seem to want to post into the chat?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ready to kick yourself?
    Code:
        message = "/em"+responcesAuto[ind]+"";
    If you would learn to use any of the debuggers, that error would have popped right out at you.

    But your code will only send *ONE* message, not repeating messages.

    You need to use setInterval instead of setTimeout if you want it to keep repeating. Also, 60000 is one minute, not 5 minutes.

    Finally, although your setTimeout as coded will work, it's kind of silly to create a function just for the purpose of calling another function.

    Try
    Code:
    setTimeout( speakAuto, 60000 );
    Or, if you really want it to happen every five minutes, over and over
    Code:
    setInterval( speakAuto, 300000 );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    originnrg (04-30-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, and what is the point of adding +"" to the end of message? In what way do you think that will change the contents of message?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oh dear that was embarrassing!!

    Thanks for that, yes the +"" was because more stuff was going to go afterwards originally. But that's removed now. I have set it now to 5 minutes per random message, was 1 minute to test it out, instead of having to wait 5 minutes.

    I have cleaned it all up, and it works perfectly! Thanks!!


  •  

    Posting Permissions

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