Uncaught TypeError: OneSignal.once is not a function
Uncaught TypeError: OneSignal.once is not a function
嗨,我想将推送通知发送到浏览器。所以我使用了一个信号。
var OneSignal = OneSignal || [];
OneSignal.push(["init", {
appId : "xxxxxxx",
autoRegister : false, /* Set to true to automatically prompt visitors */
subdomainName : "https://foxtaxi.onesignal.com",
notifyButton : {
enable: true /* Set to false to hide */
}
}]);
OneSignal.push(function() {
OneSignal.once("init", function(event) {
alert("hhh");
});
});
但我收到 未捕获类型错误:OneSignal.once 不是函数。如果有人知道,请帮助我。
解决方案
您需要使用 OneSignal.push(function() { ... })
:
来结束您的通话
OneSignal.push(function() {
/* ... */
});
此外,OneSignal.once('init')
不是有效事件。 supported events are described here.
说明
OneSignal SDK 在您的页面上异步加载,例如:
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async"></script>
您可以阅读有关异步属性的更多信息 here and here。
基本上,没有 async
属性的脚本会在获取和执行页面的其余部分时停止加载页面的其余部分,但是 async
脚本允许加载页面的其余部分,而最终 获取并执行。这对依赖于现有 OneSignal
的页面脚本提出了问题。如果OneSignal
这个变量还不存在,OneSignal
怎么用呢?
大多数 OneSignal 代码示例以:
开头
var OneSignal = window.OneSignal || [];
这表示:创建一个 OneSignal 变量,如果 OneSignal 已加载,则已将其分配给加载的实例,否则让 OneSignal 等于空数组 []
。
所有数组都有一个 .push()
function,所以在这一点上,OneSignal 变量只是我们向其推送的函数数组。
当 SDK 最终加载时,SDK processes all the functions pushed so far and redefines .push()
。
OneSignal.push(function() {
OneSignal.once("init", function(event) {
// This callback fires only once when the event occurs, and does not refire
});
});
嗨,我想将推送通知发送到浏览器。所以我使用了一个信号。
var OneSignal = OneSignal || [];
OneSignal.push(["init", {
appId : "xxxxxxx",
autoRegister : false, /* Set to true to automatically prompt visitors */
subdomainName : "https://foxtaxi.onesignal.com",
notifyButton : {
enable: true /* Set to false to hide */
}
}]);
OneSignal.push(function() {
OneSignal.once("init", function(event) {
alert("hhh");
});
});
但我收到 未捕获类型错误:OneSignal.once 不是函数。如果有人知道,请帮助我。
解决方案
您需要使用 OneSignal.push(function() { ... })
:
OneSignal.push(function() {
/* ... */
});
此外,OneSignal.once('init')
不是有效事件。 supported events are described here.
说明
OneSignal SDK 在您的页面上异步加载,例如:
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async"></script>
您可以阅读有关异步属性的更多信息 here and here。
基本上,没有 async
属性的脚本会在获取和执行页面的其余部分时停止加载页面的其余部分,但是 async
脚本允许加载页面的其余部分,而最终 获取并执行。这对依赖于现有 OneSignal
的页面脚本提出了问题。如果OneSignal
这个变量还不存在,OneSignal
怎么用呢?
大多数 OneSignal 代码示例以:
开头var OneSignal = window.OneSignal || [];
这表示:创建一个 OneSignal 变量,如果 OneSignal 已加载,则已将其分配给加载的实例,否则让 OneSignal 等于空数组 []
。
所有数组都有一个 .push()
function,所以在这一点上,OneSignal 变量只是我们向其推送的函数数组。
当 SDK 最终加载时,SDK processes all the functions pushed so far and redefines .push()
。
OneSignal.push(function() {
OneSignal.once("init", function(event) {
// This callback fires only once when the event occurs, and does not refire
});
});