How to stop setinterval function in javascript without button

I am reading an Excel file through javascript ..

<button onclick="myStopFunction()">Stop Meter</button> var myVar=setInterval(function(){readdata(1,2)},2000); function myStopFunction() { clearInterval(myVar); } 

code to read excel file

 var xVal = 1; var yVal = 2 function readdata(x,y) { x = xVal; y = yVal; try { var excel = new ActiveXObject("Excel.Application"); excel.Visible = false; var excel_file = excel.Workbooks.Open("D:\\Test1.xls");// alert(excel_file.worksheets.count); var excel_sheet = excel_file.Worksheets("Sheet1"); var data = excel_sheet.Cells(x, y).Value; //alert(data); drawWithexcelValue(data); xVal = xVal + 1; excel.Application.Quit(); } catch (ex) { alert(ex); } } 

Now I want to get rid of the stop counter in such a way that when the excel value is empty or null, it will exit. How to do this I tried something like this

  if(xVal==null& xVal=="") { clearInterval(myVar); } 

but i did not succeed

+4
source share
2 answers

Try the following:

 xVal = xVal + 1; if (data === null || data === '') { myStopFunction(); } excel.Application.Quit(); 

those. add if(){} between the lines xVal... and excel...

Actually, it looks like you need to check if there is a value in data before the line drawWithexcelValue(data); if you do not want to "draw" an empty / null value.

+3
source

It should be:

 if(xVal==null || xVal=="") { clearInterval(myVar); } 

& binary AND , you want boolean OR , which || .

+3
source

Source: https://habr.com/ru/post/1492776/


All Articles