gpt4 book ai didi

javascript - 根据其实现在不同的浏览器版本上使用 Notification.requestPermission

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:24:53 28 4
gpt4 key购买 nike

我遇到一个问题,一个函数的实现在较新的浏览器中发生了变化。我想根据实现相应地调用该函数。

Notification.requestPermission().then(function (permission) {...})

这以前被称为,

Notification.requestPermission(callback);

我调用的是初始方式,因此在较旧的浏览器中它被破坏了,因为在它们的实现中没有返回 promise 。

最佳答案

对此进行拍摄。看起来您需要有一个带有标志的有条件触发的函数。

// Outer function to prevent scope pollution
(function(){
function thingThatINeed() {}
// Flag to keep track of whether it is called.
var isCalled = false;
function conditionalCall(){
if(!isCalled) {
// if the conditionalCall function is called more than once, the
// needed function will still only be called once.
thingThatINeed();
isCalled = true;
}
}
// Call requestPermission with the deprecated form
var promise = Notification.requestPermission(conditionalCall);

// if a promise is returned, then use the promise.
if(promise){
promise.then(conditionalCall);
}
}());

关于javascript - 根据其实现在不同的浏览器版本上使用 Notification.requestPermission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48808168/

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