多次调用setInterval然后清除间隔,轮询未停止

时间:2018-10-19 06:03:12

标签: javascript setinterval polling

由于我的循环是如此之快,所以时间间隔重叠并且无法停止一个timerId。这是我的代码:

data = ['115536', '117202']; // BARCODES AVAILABLE ON A4 SHEET //

var scan_delay = 500; // USER AVG SCANNING SPEED PER BARCODE //
var timerId;

var scannedItemsList = []; // ITEMS WHICH ARE SCANNED BY SEEING A4 SHEET BY THE USER //
var tableDataList = []; // TO SHOW DATA WHICH WE GOT FROM API //

Jbin

try {
var data = ['115536', '117202']; // BARCODES AVAILABLE ON A4 SHEET //

var scan_delay = 500; // USER AVG SCANNING SPEED PER BARCODE //
var timerId;

var scannedItemsList = []; // ITEMS WHICH ARE SCANNED BY SEEING A4 SHEET BY THE USER //
var tableDataList = []; // TO SHOW DATA WHICH WE GOT FROM API //



execute(data);

function execute(data) {
    var i = 0;
    scanSimulatorWithADelay(data, i);
}

function scanSimulatorWithADelay(data, i) {
    setTimeout(function () {
        getJobDetailsByCallingAPI(data[i], i);
        i++;

        if (data.length > i) {
            scanSimulatorWithADelay(data, i);
        } else {
            i = 0;
        }
    }, scan_delay);
}

function getJobDetailsByCallingAPI(jobNumber, index) {

    scannedItemsList.push(jobNumber);

    //poll_for_jobs_count_which_are_scanned_but_waiting_to_add_to_table
    startPolling();

    //Simulate API to get response after every 3 seconds//
    var apiDelay = (index + 1) * 3000;
    setTimeout(function () {
        console.log('API CALLED AT ' + new Date().toLocaleTimeString());
        CallTheAPI(jobNumber);
    }, apiDelay);

}

function CallTheAPI(jobNumber) {
    console.log("JOB NO " + jobNumber + " API response Recd");
    tableDataList.push(jobNumber);

}

function startPolling() {
    var pollStatus = '';
    timerId = setInterval(() => {
        debugger;
        console.log('timerId when starting interval ' + timerId);
        var jobsWhichAreScannedButNotLoaded = jobsWhichAreScannedButNotLoadedStill();
        console.log("$$$$$$ jobsWhichAreScannedButNotLoaded = " + jobsWhichAreScannedButNotLoaded.length);
        if (jobsWhichAreScannedButNotLoaded.length === 0) {
            console.log("@@@ Inteval Cleared @@@ " + timerId);

            //CLEAR TIMER
            clearInterval(timerId);

        } else {
            pollStatus = 'Polling inprogress and the pollID ' + timerId;
        }
        console.log('####' + pollStatus);
    }, 2000);

}

function jobsWhichAreScannedButNotLoadedStill() {
    let stillLoadingJobs = [];

    scannedItemsList.forEach(scannedItemsListJobNumber => {
        let foundJobInsideTable = false;
        if (scannedItemsListJobNumber) {
            foundJobInsideTable = tableDataList.indexOf(scannedItemsListJobNumber) > -1;
            if (!foundJobInsideTable) {
                stillLoadingJobs.push(scannedItemsListJobNumber);
            }
        }

    }); // End of scannedItemsList forEach loop 

    if (stillLoadingJobs.length > 0) {
        return stillLoadingJobs;
    }

    return [];
}
} catch (error) { throw error; }

1 个答案:

答案 0 :(得分:1)

您的timer_id变量在全局范围内,因此每次您调用startPolling时都会被覆盖。

因此,当您致电clearInterval(timer_id)时,timer_id将是最后一个setInterval的ID,而第一个将不断运行。

只需在var函数中添加startPolling,以使timer_id的范围正确,并且下次调用不会覆盖它。

try {var data = ['115536', '117202'];

var scan_delay = 500;
// remove this one
//var timerId;

var scannedItemsList = [];  
var tableDataList = []; 



execute(data);

function execute(data) {
  var i = 0;
  scanSimulatorWithADelay(data, i);
}

function scanSimulatorWithADelay(data, i) {
  setTimeout(function () {
    getJobDetailsByCallingAPI(data[i], i);
    i++;

    if (data.length > i) {
      scanSimulatorWithADelay(data, i);
    } else {
      i = 0;
    }
  }, scan_delay);
}

function getJobDetailsByCallingAPI(jobNumber, index) {

  scannedItemsList.push(jobNumber);

  //poll_for_jobs_count_which_are_scanned_but_waiting_to_add_to_table
  startPolling();

  //Simulate API to get response after every 3 seconds//
  var apiDelay = (index + 1) * 3000;
  setTimeout(function () {
    console.log('API CALLED AT ' + new Date().toLocaleTimeString());
    CallTheAPI(jobNumber);
  }, apiDelay) ;

}

function CallTheAPI(jobNumber) {
  $.ajax({
    url: "https://jsonplaceholder.typicode.com/todos/1",
    type: "GET",
    async: true,
    success: function (response) {
      console.log("JOB NO " + jobNumber + " API response Recd");
                    tableDataList.push(jobNumber);
    }
  });

}

function startPolling() {
  var pollStatus = '';
/////////
///HERE
/////////
// Declare timerId in startPolling scope
/////////
  var timerId = setInterval(() => {
    debugger;
    console.log('timerId when starting interval '+ timerId);
    var jobsWhichAreScannedButNotLoaded = jobsWhichAreScannedButNotLoadedStill();
    console.log("$$$$$$ jobsWhichAreScannedButNotLoaded = "+ jobsWhichAreScannedButNotLoaded.length);
    if (jobsWhichAreScannedButNotLoaded.length === 0) {
      console.log("@@@ Inteval Cleared @@@ "+ timerId);

      //CLEAR TIMER
      clearInterval(timerId);
      
    } else {
      pollStatus = 'Polling inprogress and the pollID ' + timerId;
    }
    console.log('####' + pollStatus);
  }, 2000);

}

function jobsWhichAreScannedButNotLoadedStill() {
  let stillLoadingJobs = [];

  scannedItemsList.forEach(scannedItemsListJobNumber => {
    let foundJobInsideTable = false;
    if (scannedItemsListJobNumber) {
      foundJobInsideTable = tableDataList.indexOf(scannedItemsListJobNumber) > -1;
      if (!foundJobInsideTable) {
        stillLoadingJobs.push(scannedItemsListJobNumber);
      }
    }

  }); // End of scannedItemsList forEach loop 

  if (stillLoadingJobs.length > 0) {
    return stillLoadingJobs;
  }

  return [];
}
} catch (error) { throw error; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>