简体   繁体   中英

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.

You need to invoke requestAnimationFrame repeatedly, for example:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM