Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Running a random select and timer function together in javascript

I'm currently working on a twitter bot with javascript, node and the twit package. I have set up a timer and a random select function, so my aim is that the bot tweets every 60sec and chooses a random sentence from my array. When I test both bits alone (the timer OR the random function) they work but for some reason I can't make them work in conjunction. I tried to declare a variable for the getRandomSentence() function (like var tweet = getRandomSentence(tweet)) but then I would get this error return sentences[index]; ^

TypeError: Cannot read property 'NaN' of undefined

The code like I've uploaded it now runs but the timer doesn't seem to work.

// timer
setInterval(getRandomSentence, 1000*60)

// random
  var sentences = [

  'sentence1',

  'sentence2',

  'sentence3.',

  'sentence4',

  'sentence5',

  'sentence6'

    ],
    maxSentences = sentences.length;

    function getRandomSentence() {
        var index = Math.floor(Math.random() * (maxSentences - 1));
        return sentences[index];
    }

//  tweets
//
T.post('statuses/update', { status: getRandomSentence()}, tweeted);

function tweeted (err, data, response) {
if (err) {
 console.log(err);
}
else {
 console.log("It works.");
 }
}

I would really appreciate if someone could help me here! I'm still an absolute newbie.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Hi have made some changes to the code. This works to console log a random sentence to tweet every 60 seconds:

var sentences = [
    'sentence1',
    'sentence2',
    'sentence3',
    'sentence4',
    'sentence5',
    'sentence6'
];
var maxSentences = sentences.length;

var sentenceToTweet = '';

function getRandomSentence() {
    var index = Math.floor(Math.random() * (maxSentences - 1));

    sentenceToTweet = sentences[index];

    console.log(sentenceToTweet);

    setTimeout(getRandomSentence, 1000 * 60);
}

getRandomSentence();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda