Blazorserver如何从JSR调用JS事件中取消订阅/处置?

发布于 2025-01-30 14:56:31 字数 3137 浏览 2 评论 0原文

嗨,我有一个用户是无效的 - >我的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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文