Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if PayPal Smart Payment Button has finished rendering

I'm trying to add the PayPal Smart Payment button to my website. The HTML container for rendering the button is received through an AJAX request with the paypal.Buttons.render() method called onsuccess of the AJAX request. Now everything works well, except the button takes some time render on the site and become active. I'd like to hint my users that the button is rendering or loading, so they don't stay in the dark when the AJAX request returns and no button is shown. Is there a way to know when the button has completely rendered?

$.ajax({
  url: example/foler,
  data: data,
  success: success(data)
});

function success(data) {
  // data = <div id='paypal-button-container'></div>
  paypal.Buttons().render('#paypal-button-container');

  // Display "Button Loading..."
  // Find out if button has completely rendered, then turn off "button loading..."
}
like image 856
Cedric Ipkiss Avatar asked Dec 31 '22 10:12

Cedric Ipkiss


1 Answers

You can use .then(callback) since the render() returns a Promise.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

A Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of already-created promises, this guide will explain consumption of returned promises before explaining how to create them.

Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.

$.ajax({
  url: example/foler,
  data: data,
  success: success(data)
});

function success(data) {
  // data = <div id='paypal-button-container'></div>
  // Display "Button Loading..."
  paypal.Buttons().render('#paypal-button-container').then(function() { 
    // Button has completely rendered, then turn off "button loading..."
  });

}
like image 154
vhoang Avatar answered Jan 03 '23 00:01

vhoang