gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:49 25 4
gpt4 key购买 nike

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

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

最佳答案

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

}

关于javascript - 如何知道 PayPal Smart Payment Button 是否完成渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com