简体   繁体   中英

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. I want if i refresh it 3-4 times continuously then it should be called only the first time.

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; on first refresh and then check its value inside the function if its 1 then execute otherwise dont.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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