user3163916
user3163916

Reputation: 328

setInterval to requestAnimationFrame

how to convert the following setInterval() to requestAnimationFrame()?

Below is a simple working setInterval model that I'm using.

$(document).ready(function() {
    $("#add").click(function() {
         $('#result').html(
             (parseFloat($('#numA').val()) + 
              parseFloat($('#numB').val())).toString()
         );
    });
    setInterval(function () {$("#add").click()}, 1000);
});

http://jsfiddle.net/nqqfq/4/

I've seen one that requires loop (game loop), but my code isn't about gaming. It's about parsing strings.

The documentation is a bit complicated. I've tried some trial-errors, but no success so far.

Thank you for any input.

Upvotes: 0

Views: 253

Answers (1)

elclanrs
elclanrs

Reputation: 94131

You need to invoke requestAnimationFrame repeatedly, for example:

!function frame() {
  $('#add').click()
  requestAnimationFrame(frame)
}()

Demo: http://jsfiddle.net/nqqfq/6/

Upvotes: 1

Related Questions