0

I am quite new on javascript and jquery. I have implemented the following simple code to update the textfield with random values with every 5 seconds. However, it does not update.

Please do not offend if the question is so simple for you.

  <input type="text" value="Hello there" id="me"/><br/>

   self.init = function () {
        $("#me").val(setInterval(myFunction(),5000));
    }

    function myFunction() {
        return Math.floor((Math.random() * 100) + 1);
    }
2
  • 1
    setInterval will run after (5 seconds) you attempt to set the value. Instead, try setInterval(function(){$('#me').val(myFunction())}, 5000) Commented Oct 23, 2014 at 19:49
  • I could not get your point. Commented Oct 23, 2014 at 19:50

1 Answer 1

2

This is how it's done.

setInterval(function() {
  $("#me").val(myFunction());
}, 5000);

function myFunction() {
  return Math.floor((Math.random() * 100) + 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="Hello there" id="me" />

Sign up to request clarification or add additional context in comments.

1 Comment

That is great! SOF does not allow me to accept your answer right away. I should wait 10 min. Thanks a lot Preston.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.