如何知道 PayPal Smart Payment Button 是否完成渲染

How to know if PayPal Smart Payment Button has finished rendering

我正在尝试 add the PayPal Smart Payment button to my website。用于呈现按钮的 HTML 容器通过 AJAX 请求接收,其中 paypal.Buttons.render() 方法调用 AJAX 请求的 onsuccess。现在一切正常,除了按钮需要一些时间在网站上呈现并激活。我想提示我的用户按钮正在呈现或加载,因此当 AJAX 请求 returns 并且没有显示按钮时,他们不会蒙在鼓里。有没有办法知道按钮何时完全呈现?

$.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..."
}

这里有一些优化按钮呈现的建议​​方法: https://developer.paypal.com/docs/checkout/troubleshoot/performance/

您要通过 Ajax 获取什么数据?

如果您在显示按钮之前等待某些条件,请预渲染隐藏的容器和按钮,然后在成功回调时显示它们。

或者,如果按钮作为 iframe 加载 - 在这种情况下将其包装在 div 中,然后只需使用 css 将加载 gif 指定为 iframe 上的背景图像。


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

您可以使用 .then(callback),因为 render() returns 是一个 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..."
  });

}