Angular $timeout within loop fails - angularjs

I am using angular $timeout as,
$scope.alltexts = ["hii" , "hello" , "hehe"]
var sendtime = 60000
for (var i = 0; i < $scope.alltexts.length; i++) {
var text = $scope.alltexts[i]
$setTimeout(function() {}, (function(){$scope.addtext(text)}, sendtime + (i * 60000)));
};
$scope.addtext = function(text){
console.log(text)
}
But after each one minute it only console "hehe". Means it considers only last value. Please let me know how should I get proper results.

Too much of closures today, you are always passing the last index... a closure will create a new scope for each iteration.
$scope.alltexts = ["hii" , "hello" , "hehe"]
var sendtime = 60000
for (var i = 0; i < $scope.alltexts.length; i++) {
(function(i){
var text = $scope.alltexts[i]
$setTimeout(function() {}, (function(){$scope.addtext(text)}, sendtime + (i * 60000)));
})(i)
};
$scope.addtext = function(text){
console.log(text)
}

Try this
$scope.alltexts = ["hii" , "hello" , "hehe"]
var sendtime = 60000
for (var i = 0; i < $scope.alltexts.length; i++) {
(function(i){
var text = $scope.alltexts[i]
$setTimeout(function() {}, (function(){$scope.addtext(text)}, sendtime + (i * 60000)));
})(i)
};
$scope.addtext = function(text){
console.log(text)
}

Related

Console returning baddata

I cannot figure out why this http post is returning bad data in console :-
var dataObj = {"id": 21};
$http.post('http://localhost:9099/GetItems', dataObj).then(function(response) {
var size = response.data.length;
for (var i = 0; i < size; i++) {
var list = response.data.split(",");
for (var j = 0; j < list.length; j++) {
var elem = angular.element("#" + s);
elem.style.visible = "block";
}
}
});
I changed GetItems to a different api - this had no problem.
Looks like you are trying to get data from back end belongs to the id.
To get items use get instead of post
Like $http.get('http://localhost:9099/GetItems' + id)

TypeError: Impossible to find getCell function in the Sheet object

I'm trying to get some data from a Google Sheet to insert in two arrays with a for loop but I think I'm using the wrong methods.
This is the code:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var CValue = new Array();
var CName = new Array();
CValue.lenght = datAgg.getRange("B1").getValue();
CName.lenght = CValue.lenght;
for (var i = 0; i <= CValue.lenght; i++)
{
CValue[i] = datAgg.getCell(i, 16).getValue();
CName[i] = datAgg.getCell(i+2, 1).getValue();
}
var riga = f5.getRange("B2").getValue();
for (x = 3; x <= numCrypto; x++);
for (i=0;i<numCrypto;i++);
{
f5.getRange(riga, x).setValue(valCrypto[r+i]);
f5.getRange(2,numCrypto).setValue(nomiCrypto[r+i]);
}
}
The error:
TypeError: Impossible to find getCell function in the Sheet object
Thanks for your help!
Errors:
getCell() is a method of Range, not Sheet
Google Sheets starts from rowIndex = 1
If you don't need an offset (i.e. start from a certain cell), then you can use getRange:
for (var i = 0; i <= CValue.lenght; i++)
{
var r = i + 1; // rowIndex
CValue[i] = datAgg.getRange(r, 16).getValue();
CName[i] = datAgg.getRange(r+2, 1).getValue();
}
this is the solution:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var riga = f5.getRange(2, 2).getValue();
for (var i = 2; i <= 6; ++i) {
f5.getRange(riga, i + 1).setValue(datAgg.getRange("P" + i).getValue());
f5.getRange(2, i + 1).setValue(datAgg.getRange("A" + i).getValue());
}
}
i posted another question here for the same problem and I get a perfect answer how to use for loop to drastically reduce code lenght.
the only part I changed is this one: (var i = 2; i <= 6; ++i) with this one: (var i = 2; i <= n+1; ++i), so it can work perfectly with any variable n to get with command "var numCrypto = datAgg.getRange(1,2).getValue();"

For-loop return array value

I'm trying to insert url for menu through mustache template. But just the first value is being returned for the array.
Or is this the return method wrong
var main_menu_link = ["main_dashboard.html", "#", "online_dashboard.html","index.html","#","#","#"];
var url = "";
var i;
var url_link="";
for(i = 0; i < main_menu_link.length; i++) {
url += main_menu_link[i];
return '' + text + '';
}
CodePen working here
The return statement has to be after the loop:
var main_menu_link = ["main_dashboard.html", "#", "online_dashboard.html","index.html","#","#","#"];
var url = "";
var i;
var url_link="";
for(i = 0; i < main_menu_link.length; i++) {
url += '' + text + '';
}
return url;
Correct template as below in-case it can be of use to someone else
var link_details = { "link_details" :[
{ main_menu: "Dashboard", main_menu_link: "dashboard.html" },
{ main_menu: "Analytics", main_menu_link: "#" },
{ main_menu: "System", main_menu_link: "system.html" }
]};
var template = "<ul>{{#link_details}}<li>{{main_menu}}</li>{{/link_details}}‌​</ul>";
var html = Mustache.to_html(template, link_details);
document.write(html)

Google Apps Scrit add value to array every time a loop executes

I would like to add "RealData" to my array "tempArr", I am trying to use "push" but I have no experience with it so I can not get it to work. Could you help me out?
for (var i=0; i < data.length; i++) {
var testDate = subDaysFromDate(data[i][0],1)
var DateToCheck = Utilities.formatDate(testDate, "GMT", "dd-MM-yyyy");
var DateToCheckBefore = DateToCheck.split("-");
var DateToCheckAfter = new Date(DateToCheckBefore[2], DateToCheckBefore[1]-1, DateToCheckBefore[0]); //dit is de datum van een row
Row++;
var tempArr = new Array();
var d1 = $d1.split("-");
var d2 = $d2.split("-");
var from = new Date(d1[0], d1[1]-1, d1[2]); // -1 because months are from 0 to 11
var to = new Date(d2[0], d2[1]-1, d2[2]);
if(DateToCheckAfter >= from && DateToCheckAfter <= to){
var KlantNr = sheet.getRange("A"+Row).getValue();
var KlantVoornaam = sheet.getRange("B"+Row).getValue();
var KlantAchternaam = sheet.getRange("C"+Row).getValue();
var HTMLData = KlantNr + "-" + KlantVoornaam + "-" + KlantAchternaam;
var RealData = HTMLData.split("-");
tempArr.push(RealData);
}
}
Logger.log(tempArr);
When you PUSH an array into another you're referencing it, to copy use slice:
tempArr.push(RealData.slice(0));
Also, this is a javascript problem.

Angular E2E- Is it possible to call an 'It' function in a loop?

This works
it('should create a report for index: ', function() {
var dataLength = window.test.parameters.length;
for (var i = 0; i < dataLength; i++){
var parameterObject = window.test.parameters[i];
var propNumber = parameterObject.propertyNumber;
element(":contains('" + propNumber + "')").click();
element(":button('View')").click();
expect(element('div *:contains(' + reportName + ')').count()).toEqual(4);
}
});
This does not
var dataLength = window.test.parameters.length;
for (var i = 0; i < dataLength; i++){
it('should create a report for index: ', function() {
var parameterObject = window.test.parameters[i];
var propNumber = parameterObject.propertyNumber;
element(":contains('" + propNumber + "')").click();
element(":button('View')").click();
expect(element('div *:contains(' + reportName + ')').count()).toEqual(4);
});
};
My intention is to parameterize this test to run it many times with different combinations of parameters each time.
I've tried removing var and 'use strict'; to make sure i is global. I've tried declaring i outside of the 'describe' function. Is there a trick to this?
it may be async, try to lexically scope it.
var dataLength = window.test.parameters.length;
for (var i = 0; i < dataLength; i++){
it('should create a report for index: ', function(i) {
return function () {
var parameterObject = window.test.parameters[i];
var propNumber = parameterObject.propertyNumber;
element(":contains('" + propNumber + "')").click();
element(":button('View')").click();
expect(element('div *:contains(' + reportName + ')').count()).toEqual(4);
};
}(i));
};

Resources