简体   繁体   English

当函数第二次运行时,检查记录ID是否不同? 的JavaScript

[英]Check if record ID is different when function runs second time? JavaScript

I have implemented beforeunload function in my single page app. 我已经在单页应用程序中实现了beforeunload功能。 This seems very cool because we can check when user wants to close their browser/window. 这似乎很酷,因为我们可以检查用户何时要关闭其浏览器/窗口。 This was very important in my app. 这在我的应用中非常重要。 App has locking feature that prevents multiple users editing same record at the same time. 应用具有锁定功能,可防止多个用户同时编辑同一条记录。 However this cause some other problems like user simply closing their browser and record would stay locked in that case. 但是,这会导致其他一些问题,例如用户仅关闭浏览器并保持记录状态即可。 So after I implemented beforeunload every time user click's on X to close their browser or window function to unlock the record will run. 因此,在每次用户单击X以关闭其浏览器或窗口功能以解锁记录后,我实施beforeunload都将运行。 Everything seemed fine until I tried to use Logoff button in my app. 一切似乎都很好,直到我尝试在应用程序中使用注销按钮。 This cause the problem. 这会导致问题。 As soon as click on the logoff button my unlock function will run then beforeunload will be triggered and function will run second time using same recordID. 一旦单击注销按钮,我的解锁功能将运行,然后触发beforeunload并且该功能将使用相同的recordID第二次运行。 I tried to ignore beforeunload once user clicks on the Logoff but that didn't work since Logoff is in the separate iframe. 用户单击注销后,我试图忽略beforeunload ,但由于注销位于单独的iframe中,因此无法正常工作。 I was wondering if there is a way to check recID every time before my unlock function runs. 我想知道是否有一种方法可以在我的解锁功能运行之前每次都检查recID If ID is the same as the previous ID I should skip and not run the function. 如果ID与先前的ID相同,则应跳过而不运行该函数。 Here is example of my function: 这是我的功能示例:

window.onbeforeunload = function () {
    var recID = $.trim($("#recordID").val());
    //Here I should check if recID is the same or different. If function runs more than once recID always should be compared with the previous.
    if(recID){
        removeUnlock(recID); //Call function to remove record.
    }

    return 'Are you sure you want to leave?';
}

If anyone knows how this can be achieved or better way to handle this situation please let me know. 如果有人知道如何实现此目标或更好的方式来处理这种情况,请告诉我。 Thank you! 谢谢!

It is possible to communicate between iframes with messages How to communicate between iframe and the parent site? 可以在iframe与消息之间进行通信如何在iframe与父网站之间进行通信? or through session storage if your pages are in the same domain sessionStorage in iframe . 或通过会话存储(如果您的页面位于iframe中的同一域sessionStorage中)

So you should probably send a message from log off iframe if logout went correctly and save this information while listening to those event in your code. 因此,如果注销正确,您应该应该从iframe注销中发送一条消息,并在侦听代码中的事件时保存此信息。 Then add additional condition in onbeforeunload. 然后在onbeforeunload中添加其他条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM