简体   繁体   English

如何允许在jquery中一次只刷新访问一种方法

[英]how to allow one method to be accessed by only refresh at a time in jquery

i have one method which is called when ever i refresh my page and that method saves a value in the database.Now if i refresh the same page continuously 3-4 times then the value is stored 3-4 times in database.我有一种方法,每当我刷新页面时都会调用该方法,该方法会在数据库中保存一个值。现在,如果我连续刷新同一页面 3-4 次,则该值将在数据库中存储 3-4 次。 I want if i refresh it 3-4 times continuously then it should be called only the first time.我想如果我连续刷新它 3-4 次,那么它应该只在第一次被调用。

window.onbeforeunload = confirmClose;

function confirmClose() {
  var refreshTime = new Date().getTime();
  if (!isNaN(keyerAudit.auditStartTime) && (keyerAudit.auditStartTime != 0))
    sessionStorage.setItem("FIRSTVISIT", refreshTime);
  var refreshAuditTime = 0;
  if (!isNaN(keyerAudit.auditStartTime) && (keyerAudit.auditStartTime != 0))
    refreshAuditTime = (refreshTime - keyerAudit.auditStartTime) / 1000.0;
  if (!isNaN(refreshAuditTime) && (refreshAuditTime != 0)) {
    sessionStorage.setItem("DB", refreshAuditTime);
    $.ajax({
      url: '/auditTimecalculate',
      type: 'POST',
      async: false,
      dataType: "json",
      data: {
        auditTime: refreshAuditTime
      },
      success: function(data) {
        console.log("returned from /auditTimecalculate ");
        console.log("the before refresh starttime was: ", keyerAudit.auditStartTime);
        sessionStorage.setItem("FIRSTVISIT", keyerAudit.auditStartTime);
        //                 refreshTime=undefined;
        refreshAuditTime = undefined;
      }
    });
  }

You can use a session variable like session["count"] = 1;您可以使用会话变量,如session["count"] = 1; on first refresh and then check its value inside the function if its 1 then execute otherwise dont.第一次刷新,然后检查它在函数内的值,如果它是 1,则执行否则不要。

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

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