简体   繁体   English

如何知道 PayPal 智能支付按钮是否已完成渲染

[英]How to know if PayPal Smart Payment Button has finished rendering

I'm trying to add the PayPal Smart Payment button to my website .我正在尝试将 PayPal Smart Payment 按钮添加到我的网站 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.用于再现按钮的HTML容器通过与一个AJAX请求接收paypal.Buttons.render()调用的方法onsuccess AJAX请求的。 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.我想提示我的用户按钮正在呈现或加载,所以当 AJAX 请求返回并且没有显示按钮时,他们不会留在黑暗中。 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..."
}

You can use .then(callback) since the render() returns a Promise.您可以使用 .then(callback) 因为 render() 返回一个 Promise。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises 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. Promise 是表示异步操作最终完成或失败的对象。 Since most people are consumers of already-created promises, this guide will explain consumption of returned promises before explaining how to create them.由于大多数人都是已经创建的 Promise 的消费者,本指南将在解释如何创建它们之前解释返回的 Promise 的消费。

Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.本质上,promise 是一个返回的对象,您可以将回调附加到该对象上,而不是将回调传递给函数。

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

}

There are a few suggested ways to optimize rendering the button here: https://developer.paypal.com/docs/checkout/troubleshoot/performance/这里有一些优化按钮渲染的建议方法: https : //developer.paypal.com/docs/checkout/troubleshoot/performance/

What data are you fetching via Ajax?您通过 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.或者,如果按钮作为 iframe 加载 - 在这种情况下,请将其包装在 div 中,然后只需使用 css 将加载 gif 指定为 iframe 上的背景图像。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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