我创建了一个名为的文件ExtremeNotifications.js
并将其添加到_Layout.cshtml
主布局中。
ExtremeNotifications.js包含以下JavaScript代码:
var extremeNotifications = extremeNotifications || {};
extremeNotifications = (function () {
var baseURL = document.baseURI;
var url = baseURL + 'api/usernotifications';
var isNotificationServiceStarted = false;
var timer;
function getPendingNotifications() {
$.ajax({ url: url, success: dataRetrieved, type: 'GET', dataType: 'json' });
}
function dataRetrieved(data) {
alert(data);
}
function startNotifications() {
if (!isNotificationServiceStarted) {
timer = setInterval(getPendingNotifications, 3000);
isNotificationServiceStarted = true;
}
}
function stopNotifications() {
clearInterval(timer);
isNotificationServiceStarted = false;
}
return {
start: startNotifications(),
getPendingNotifications: getPendingNotifications(),
isNotificationServiceStarted: isNotificationServiceStarted,
stop: stopNotifications()
}
})();
然后在我的家中Index.cshtml
,仅在以下情况下,使用以下代码启动通知User.Identity.IsAuthenticated
:
<script>
extremeNotifications.start();
</script>
因此,现在当我的页面开始并且已通过身份验证的用户时,我会在第一时间收到警报框,但三秒钟后再也看不到任何警报。
任何意见?
您已经关闭,但是创建的返回对象不正确:
return {
start: startNotifications,
getPendingNotifications: getPendingNotifications,
isNotificationServiceStarted: isNotificationServiceStarted,
stop: stopNotifications
};
通过()
在函数名称之后添加后缀,您的代码可以调用函数并返回其返回值,而不是返回对函数本身的引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句