I want to ajax the collected access information to the server when the web page is closed. Is it practical? ?
The approximate code structure is as follows:
window.onbeforeunload=function(e){
visit_end=new Date();
visit_long=((visit_end.getTime()-visit_start.getTime())/1000).toFixed(1);
//此处省略以上采集的访问信息
ajax_visit_info(); //在关闭网页时,调用ajax函数发送到后台。
//return confirm('你真的要关闭吗?'); //不想加上这个,看起来不友好。
}
The question is, is it practical to use window.onbeforeunload to trigger ajax?
If the user's phone is stuck, or there is a problem with the network, will there be a high probability of missing the ajax trigger? ?
PS: I can test it with wamp.
你这种做法肯定会导致数据丢失的,而且一旦丢失就找不回来。
如果这些数据比较重要的话,建议存一个副本到本地缓存中,每次进来做一次时间戳比对,如果时间戳比上次提交到后台更新,则更新后台数据。
这样做能避免因网络问题导致的数据丢失。