【发布时间】:2017-09-01 01:57:34
【问题描述】:
场景:我正在创建一个活动注册模块,用户可以在其中从日历中选择一个日期进行预订,填写一些输入字段并支付预订日期。 其中一项要求是,每个日期只能有一个预订。 这会出现两个人想在同一时间预订同一日期的情况。因此,我们需要向后来预订日期的用户显示一条消息,该特定日期的预订已经在进行中。请稍后再回来查看。
为此,每当用户选择日期时,我都会在我的数据库中创建一个临时条目。针对该条目,我可以将消息显示给稍后到达的其他用户。
从逻辑上讲,如果第一个用户出现以下情况,则必须删除该条目:
- 选择其他日期。
- 关闭浏览器或离开页面
以便第二个想要在同一日期预订的用户可以使用它。
问题:一旦用户更改了他选择的日期,我就可以删除该条目。我无法实现第二个。我编写了一个函数,它将删除该行并在 onbeforeunload jQuery 事件上调用该函数。显然,该函数不适用于 jQuery。
window.onbeforeunload = function(){
deleteTempEntry(); //This sends an ajax call to delete the entry. Not working.
alert("The second alert"); // even this alert doesn't work.
// I actually intend to use confirm box to make sure to not to delete the entry if the user does not leave the page.
confirm("Your current progress will be removed. Are you sure?");
//Calling this return function shows the warning message but none of the other code works.
//return 'Are you sure you want to leave?';
};
我尝试过,绑定/开启/纯 JS,但这些似乎都不起作用。
正如here 解释的那样,由于安全原因,我对 onbeforeunload/unload 事件无能为力。 有什么解决方法吗?
谢谢。
【问题讨论】:
-
打开一个新窗口怎么样?然后在那里显示您的消息,弹出窗口上的卸载应该触发
deleteTempEntry() -
因此,当用户关闭选项卡/浏览器时,您在前端无能为力。所以你必须在后端维护一个超时功能,以便它删除不完整的注册。
标签: javascript php jquery ajax date