Blazorserver如何从JSR调用JS事件中取消订阅/处置?
嗨,我有一个用户是无效的 - >我的Blazor Server应用中的注销功能。通过JSR,我每次按下用户按下鼠标BTN或键时重置计时器。如果未完成“ iNput”,则计时器弹出并调用注销方法(在此示例中,该集合页面被导航到)。
我将此组件包括在Shared/MainLayout.Razor中。 但是,即使用户在不再使用mainlayout的页面上,也会调用注销methode(例如,logoutpage // no mainlayout)。
如何停止曾经调用的JSR事件来调用LogoutMethode?
MainLayout.Razor的范围是什么?
如果我不在Mainlayout中包含登录组件,而是将其用作“正常”页面上的组件,则可以使用预期。
代码: mainLayout.Razor:
@inherits LayoutComponentBase
<PageTitle>InActivLogOffComponent</PageTitle>
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="sticky-md-top bg-white"><MyHeader></MyHeader></div>
<article class="content px-4">
@Body
</article>
</main>
</div>
TimerComponent:
@using System.Timers
@inject IJSRuntime _jsRuntime
@inject NavigationManager _navigationManager
<div class="border border-2 border-primary m-2 p-2">
<h3>MyHeaderPage</h3>
</div>
@code {
protected override void OnInitialized()
{//the JSR-Event seems to reinstantiate this code ???
Ini();
}
public System.Timers.Timer timerObj = new();
public async void Ini()
{//Set Timer
timerObj = new System.Timers.Timer(3000);
timerObj.Elapsed += EventHandlerTimerElapsed;
timerObj.AutoReset = false;
timerObj.Start();
//observe onclick and onkeypress in JS function
await _jsRuntime.InvokeVoidAsync("timeOutCall", DotNetObjectReference.Create(this));
}
[JSInvokable]
public void TimerIntervalReset()
{// Resetting the Timer
timerObj.Stop();
timerObj.Start();
}
public void EventHandlerTimerElapsed(Object? source, ElapsedEventArgs e)
{//if Timer is elapsed ... (no mouseclick or keypress happend for XX time)
//PROBLEM NOTHING Stops the Event from happening again and again
timerObj.Elapsed -= EventHandlerTimerElapsed;
timerObj.Enabled = false;
timerObj.Dispose();
//^^
_navigationManager.NavigateTo("/counter", true);
}
}
我的JavaScript:
function timeOutCall(dotnethelper) {
//document.onmousemove = resetTimeDelay;
document.onclick = resetTimeDelay;
document.onkeypress = resetTimeDelay;
function resetTimeDelay() {
dotnethelper.invokeMethodAsync("TimerIntervalReset");
}
}
Hi I have a 'user is inactiv -> logout' feature in my blazor server app. Via JSR I reset a Timer everytime the user presses a Mousebtn or a Key. If no 'ínput' is done the timer elapses and the logout methode is called (in this example the counterpage is navigated to).
I included this component in the Blazor shared/mainlayout.razor.
But the logout methode is getting called even if the user is on a page that doesn't use the mainlayout anymore (e.g. logoutpage // no mainlayout).
How do I stop the once invoked JSR Event to call the logoutmethode?
What scope has the mainlayout.razor?
If I don't include the logoff component in the mainlayout and instead use it on a 'normal' page as a component it works a expected.
Code:
MainLayout.razor:
@inherits LayoutComponentBase
<PageTitle>InActivLogOffComponent</PageTitle>
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="sticky-md-top bg-white"><MyHeader></MyHeader></div>
<article class="content px-4">
@Body
</article>
</main>
</div>
TimerComponent:
@using System.Timers
@inject IJSRuntime _jsRuntime
@inject NavigationManager _navigationManager
<div class="border border-2 border-primary m-2 p-2">
<h3>MyHeaderPage</h3>
</div>
@code {
protected override void OnInitialized()
{//the JSR-Event seems to reinstantiate this code ???
Ini();
}
public System.Timers.Timer timerObj = new();
public async void Ini()
{//Set Timer
timerObj = new System.Timers.Timer(3000);
timerObj.Elapsed += EventHandlerTimerElapsed;
timerObj.AutoReset = false;
timerObj.Start();
//observe onclick and onkeypress in JS function
await _jsRuntime.InvokeVoidAsync("timeOutCall", DotNetObjectReference.Create(this));
}
[JSInvokable]
public void TimerIntervalReset()
{// Resetting the Timer
timerObj.Stop();
timerObj.Start();
}
public void EventHandlerTimerElapsed(Object? source, ElapsedEventArgs e)
{//if Timer is elapsed ... (no mouseclick or keypress happend for XX time)
//PROBLEM NOTHING Stops the Event from happening again and again
timerObj.Elapsed -= EventHandlerTimerElapsed;
timerObj.Enabled = false;
timerObj.Dispose();
//^^
_navigationManager.NavigateTo("/counter", true);
}
}
My Javascript:
function timeOutCall(dotnethelper) {
//document.onmousemove = resetTimeDelay;
document.onclick = resetTimeDelay;
document.onkeypress = resetTimeDelay;
function resetTimeDelay() {
dotnethelper.invokeMethodAsync("TimerIntervalReset");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论