最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to know if PayPal Smart Payment Button has finished rendering - Stack Overflow

programmeradmin4浏览0评论

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 bee 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 pletely 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 pletely rendered, then turn off "button loading..."
}

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 bee 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 pletely 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 pletely rendered, then turn off "button loading..."
}
Share Improve this question edited Dec 11, 2019 at 14:38 Cedric Ipkiss asked Dec 11, 2019 at 10:58 Cedric IpkissCedric Ipkiss 6,3577 gold badges49 silver badges76 bronze badges 1
  • There are a few suggested ways to optimize rendering the button here: developer.paypal./docs/checkout/troubleshoot/performance What data are you fetching via Ajax? – limco Commented Dec 11, 2019 at 14:49
Add a ment  | 

2 Answers 2

Reset to default 7 +25

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

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

A Promise is an object representing the eventual pletion 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 pletely rendered, then turn off "button loading..."
  });

}

There are a few suggested ways to optimize rendering the button here: https://developer.paypal./docs/checkout/troubleshoot/performance/

What data are you fetching via Ajax?

If you are waiting for some condition before showing the button, pre-render the container and buttons hidden, then display them upon successful callback.

Alternatively, if the button is loaded as an iframe - in that case wrap it in a div then just use css to specify a loading gif as background image on the iframe.


div.button-wrapper-div {
  background:url(../images/loader.gif) center center no-repeat;
}```
发布评论

评论列表(0)

  1. 暂无评论