Stop execute ajax in for loop - javascript

I'm trying to do the following. Get number of pages from the API. Each page has multiple results. I check all the results with my condition. If the result fits the condition, then I need to finish the check, finish the page search and pass the result to another function. I don't understand how to end ajax (getData() execution in the checkPages() function) and exit the for loop in the same place. The break and return keywords do not help. Please tell me how to do it. Maybe I need to do to refactor my code. I don't really like to "throw" results from a function into a function. I do not use async/await because I need compatibility with old browsers.
getData("url-to-get-data").done(function (result) {
checkPages(result.total_pages);
});
function getData(url) {
return $.get({
url: "url-to-get-data"
})
}
function checkPages(pagesCount) {
for (var i = 2; i <= pagesCount; i++) {
getData("url-to-get-data", i).done(function(result) {
var today = checkToday(result);
if (today != null) {
//someMethod
//how to end the getData function and the for loop
}
});
}
}
function checkToday(response) {
var results = response.results;
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
for (var i = 0; i < results.length; i++) {
var d = new Date(results[i].release_date);
if (d.getDate() === day && d.getMonth() === month) {
return results[i];
}
}
return null;
}

simplest change to your checkPages function
inner function that calls itself as required
function checkPages(pagesCount) {
function checkPage(i) {
if (i <= pagesCount) {
getData("url-to-get-data", i).done(function(result) {
var today = checkToday(result);
if (today == null) { // only get next page if today is null
checkPage(i+1);
}
});
}
}
checkPage(2);
}

If I understand correctly you are trying to do something like this?
UPDATE: implemented que to check if request is finsihed
getData("url-to-get-data").done(function (result) {
checkPages(result.total_pages);
});
function getData(url) {
return $.get({
url: "url-to-get-data"
})
}
function checkPages(pagesCount) {
let doContinue = true;
let loading = false;
let i = 2;
var checker = setTimeout(()=>{
if(i > pagesCount) clearTimeout(checker);
if(!loading){
loading = true;
getData("url-to-get-data", i).done(function(result) {
var today = checkToday(result);
if (today != null) {
clearTimeout(checker);
}
i++;
loading = false;
});
}
},100);
}
function checkToday(response) {
var results = response.results;
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
for (var i = 0; i < results.length; i++) {
var d = new Date(results[i].release_date);
if (d.getDate() === day && d.getMonth() === month) {
return results[i];
}
}
return null;
}

Make your ajax call synchronous or use callback functions to keep getting more data until conditions are met.

Related

mutliple set timeouts running at once

so, i have a for loop that I want to run multiple set timeouts at once so it can continually check if it is a certain time for multiple times. i can't just check for all multiple times all at once, the check needs to repeat after different amounts of time. it only continues to repeat the last iteration of the loop, the last dict in lat.
my approach simplified a bit:
lat = list of dictionaries with some values that differentiate them + times
for(i = 0; i< lat.length; i++){
if(lat[i][differentiate value]) = {
function checktime(){
if(currenttime != lat[i]){
setTimeout(checktime,30000)
}
else{
console.log("yay the time is right!")
}
}
else if(lat[i][differentiate value]) = {
function checktime(){
if(currenttime != lat[i]){
setTimeout(checktime,50000)
}
else{
console.log("yay the time is right!")
}
}
}
How would I go about making this (its for a notification app)
original code:
(each value in later looks like [[75,null,[7,28,2021]], null,[[9,52,"p"]],"e&r"] with the amount of notifications, a preset date, the date for these reminders to be on, by time, which would be the first if, and the array of at times. for testing I had 2 laters with 2 different at times):
chrome.storage.sync.get('later', function(data){
if (data.later !== null){
var lat = data.later;
for(i = 0; i< lat.length; i++){
var currentDict = lat[i];
if(currentDict['notis'][1] !== null){
console.log("cheese")
var by = currentDict['notis'][1];
console.log(by)
const d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
var da = currentDict['notis'][0][2][1];
var mo = currentDict['notis'][0][2][0];
var ye = currentDict['notis'][0][2][2]
var h = by[0];
var m = by[1];
var ampm = by[2];
if(ampm == "p"){
h = h + 12;
}
var byMS = h*3600000 + m*60000;
var currentMS = hr*3600000 + min*60000 + sec*1000;
//check if right date then check if time is lesss than
function checkdate(){
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if(da == day && mo == month && ye == year){
var amt = 0;
function checktime(){
if(byMS >= currentMS){
//noti and delete
var int = setInterval(function(){
chrome.notifications.create({
title: currentDict['name'],
message: "do da " + currentDict['name'],
iconUrl: "logo.png",
type: "basic"
})
amt++
console.log(amt)
const dddd = new Date();
console.log(dddd.getMinutes() + " / " + dddd.getSeconds())
if(amt >= currentDict['notis'][0][0]){
clearInterval(int)
console.log("done")
//ju finish taht
console.log(lat)
lat.splice(lat.indexOf(currentDict),1)
console.log(lat)
chrome.storage.sync.set({'later': lat})
}
}, (byMS-currentMS)/currentDict['notis'][0][0])
}
else{
setTimeout(checktime,30000)
}
}
checktime();
}
else{
setTimeout(checkdate,66400000)
}
}
checkdate();
}
else if(currentDict['notis'][2] !== null){
console.log("cheese")
var at = currentDict['notis'][2];
console.log(at)
var arrayat = [];
for(j = 0; j<= at.length-1; j++){
var atcycle = at[j];
console.log(atcycle)
const ddd = new Date();
var hr = ddd.getHours();
var min = ddd.getMinutes();
var da = currentDict['notis'][0][2][1];
var mo = currentDict['notis'][0][2][0];
var ye = currentDict['notis'][0][2][2]
var hrat = atcycle[0];
var minat = atcycle[1];
var ampm = atcycle[2];
if(ampm == "p"){
hrat = hrat + 12;
}
console.log(hrat + "/" + minat + "/" + ampm)
if(hr <= hrat){
var temparray = [];
temparray.push(hrat)
temparray.push(minat)
arrayat.push(temparray);
console.log(arrayat)
}
else if(hr == hrat){
if(min<minat){
var temparray = [];
temparray.push(hrat)
temparray.push(minat)
arrayat.push(temparray);
console.log(arrayat)}
}
}
console.log(arrayat.length)
function checkdate(){
console.log(arrayat.length)
console.log("date")
const d = new Date();
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if(da == day && mo == month && ye == year){
function check(){
console.log(arrayat.length)
console.log("check")
for(l=0; l<arrayat.length; l++){
console.log(arrayat.length)
const dd = new Date();
var hr = dd.getHours();
var min = dd.getMinutes();
console.log(arrayat[l][1])
console.log(min)
if(arrayat[l][0] == hr && arrayat[l][1] == min ){ //at one of the times
console.log(arrayat)
arrayat.splice(l,1)
console.log(arrayat)
if(arrayat.length == 0){
lat.splice(lat.indexOf(currentDict),1)
chrome.storage.sync.set({'later': lat})
console.log(lat)
}
chrome.notifications.create({
title: currentDict['name'],
message: "do da " + currentDict['name'],
iconUrl: "logo.png",
type: "basic"
})
//add noti with name and delete it
console.log(arrayat)
check();
}
}
console.log(arrayat.length)
if(arrayat.length !== 0){
console.log("and repeat")
setTimeout(check,15000);//SETINTERVAL INSTEAD? ANDCLEAR
}
}
check();
}
else{
setTimeout(checkdate,66400000)
}
}
checkdate();
}
}
}
})
}
This is the wrong approach. You know the times, so you know how far off they are in time. You don't need to check over and over again if the time has arrived. Instead work out how many milliseconds until the time in question, then set a timeout for that time.
You basically need to pass different functions to each setTimeout():
function checktime1(){
if(currenttime != lat[i]){
setTimeout(checktime1,30000)
}
else{
console.log("yay the time is right!")
}
}
function checktime2(){
if(currenttime != lat[i]){
setTimeout(checktime2,50000)
}
else{
console.log("yay the time is right!")
}
}
However, this is obviously not scalable. If you have lots of timeouts you will be writing lots of duplicate functions. Worse, if you have a dynamic number of timeouts this won't solve your problem.
Fortunately, javascript lets us declare functions inside another function:
function checktime(timeout){
function loop () {
if(currenttime != lat[i]){
setTimeout(loop,timeout)
}
else{
console.log("yay the time is right!")
}
}
loop();
}
This allows you to dynamically create functions as needed:
checktime(30000);
checktime(50000);
If you need to pass arguments to each timeouts (eg. if you need to display custom alerts) you can just pass it to your outer function and it will be captured in a closure:
function checktime(timeout, message){
function loop () {
if(currenttime != lat[i]){
setTimeout(loop,timeout)
}
else{
console.log(message)
}
}
loop();
}
Then you can do:
checktime(30000, "Yay, the time is right for timer 1");
checktime(50000, "Yay, the time is right for timer 2");
Similarly, if you need to execute custom logic for each timer you can also pass it as an argument (callback):
function checktime(timeout, callback){
function loop () {
if(currenttime != lat[i]){
setTimeout(loop,timeout);
}
else{
console.log("yay the time is right!");
callback(); // <----------------- execute custom logic
}
}
loop();
}
Which you can use like:
checktime(30000, () -> console.log("TIMER 1"));
checktime(50000, () -> alert("TIMER 2"));

Returned array from a function is not caught in another function

As the title says, even though it may be a little bit vague, when i call a function which returns an array of objects in another function, for some reason i'm not able to catch the result in another object in the current function. I have a feeling that is just a little thing that i'm missing.
Here is the function which returns the array of objects :
function GetAllNeighboors(myLocationLat, myLocationLong) {
$.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
return Neightbours;
});
}
And here is where i'm calling that function:
if (data.isClosed != true) {
map.setCenter(new google.maps.LatLng(data.latitudine, data.longitudine));
map.setZoom(17);
} else {
Vecini = new Object();
Vecini = GetAllNeighboors(data.latitudine, data.longitudine);
NOTE: The Neightbours from the first function comes with the expected result.
NOTE2: I think i figured out what is the problem, but i'm not really sure how i can solve it. The problem is i'm not waiting for the result, i should make the function asynchronous, in order to have the result in the second function. Any idea how i can do that?
You can pass a callback to your GetAllNeighbours function and set its value inside the $.get callback like this
function GetAllNeighboors(myLocationLat, myLocationLong, cb) {
$.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
cb(Neightbours);
});
}
And you now call it like this:
if (data.isClosed != true) {
map.setCenter(new google.maps.LatLng(data.latitudine, data.longitudine));
map.setZoom(17);
} else {
GetAllNeighboors(data.latitudine, data.longitudine, function(Neighboors){
Vecini = new Object();
Vecini = Neighboors
});
You arent returning anything from the first function, you are returning from the function inside of that function. Try this:
function GetAllNeighboors(myLocationLat, myLocationLong) {
return $.get("/Home/GetAllLocation",
function (data, status) {
Neightbours = new Object();
var j = 0;
for (var i = 0; i < data.length; i++) {
if (distanceBetweenTwoStations(myLocationLat, myLocationLong, data[i].latitudine, data[i].longitudine,"K") <= 1 && data[i].latitudine != myLocationLat && data[i].longitudine != myLocationLong) {
Neightbours[j] = data[i];
j++;
}
}
return Neightbours;
});
}

Angularfire $firebaseArray extend dynamic count\sum

Am trying to achieve a dynamic count of certain node if defined:
This works but its not dynamic, you have to call sum().
app.factory("ArrayWithSum", function($firebaseArray) {
return $firebaseArray.$extend({
sum: function() {
var total = 0;
var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);
// the array data is located in this.$list
angular.forEach(this.$list, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
total++;
}
}
}
});
return total;
}
});
});
I tried $$update but can't access this_counter in array:
app.factory("counter", function($firebaseArray) {
return $firebaseArray.$extend({
sum: function() {
return this._counter;
},
$$updated: function(){
var changed = $firebaseArray.prototype.$$updated.apply(this, arguments);
var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);
if( !this._counter ) {
this._counter = 0;
}
// the array data is located in this.$list
angular.forEach(this.$list, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
this._counter++;
}
}
}
});
return changed;
}
});
});
Does anyone know how to make a dynamic variable that I can update and access?
Thanks
Got it working with $firebaseObject. Obviously you can add a property _counter to an object not an array. Any way this works and a good way to get a dynamic count.
app.factory("counter", function($firebaseObject) {
return $firebaseObject.$extend({
$$updated: function(){
var changed = $firebaseObject.prototype.$$updated.apply(this, arguments);
if( !this._counter ) { this._counter = 0; }
var total = 0;
var todayDate = new Date();
var start = todayDate.setHours(0,0,0,0);
var end = todayDate.setHours(23,59,59,999);
// the array data is located in this.$list
angular.forEach(this, function(rec) {
if (angular.isDefined(rec.qa)){
if (angular.isDefined(rec.qa.completed)) {
if (rec.qa.completed >= start && rec.qa.completed <= end){
total++;
}
}
}
});
this._counter = total;
return changed;
}
});
});
vm.panels = new counter(panelsRef);
{{vm.panels._counter}}
Am having issues with watches not firing after a long duration on IE11. So thought I'd try this approach.

Date comparison in Javascript acts like a string comparison

I'm using Angular and have the following filter function in my controller:
$scope.filterDocuments = function (row) {
var dateCompare = $filter('date')(row.FilingDate, 'MM/dd/yyyy');
if (dateCompare >= $scope.dateLimit) {
if ($scope.query === '' || $scope.query === undefined) {
return true;
} else if (angular.lowercase(row.Description).indexOf($scope.query) !== -1) {
return true;
} else {
return false;
}
} else {
return false;
}
};
This function is used to filter the data during an ng-repeat. What happens is the date comparision is acting like a string comparision. I have tried changing the code to this:
$scope.filterDocuments = function (row) {
var dateCompare = $filter('date')(row.FilingDate, 'MM/dd/yyyy');
if (dateCompare.getTime() >= $scope.dateLimit.getTime()) {
if ($scope.query === '' || $scope.query === undefined) {
return true;
} else if (angular.lowercase(row.Description).indexOf($scope.query) !== -1) {
return true;
} else {
return false;
}
} else {
return false;
}
};
And that fails worse!
How to fix this so that the date comparision works and I can filter based upon the dates?
I found the answer on another thread (can't find it now). I am using the following function:
function stringToDate(_date, _format, _delimiter) {
var formatLowerCase = _format.toLowerCase();
var formatItems = formatLowerCase.split(_delimiter);
var dateItems = _date.split(_delimiter);
var monthIndex = formatItems.indexOf("mm");
var dayIndex = formatItems.indexOf("dd");
var yearIndex = formatItems.indexOf("yyyy");
var month = parseInt(dateItems[monthIndex]);
month -= 1;
var formatedDate = new Date(dateItems[yearIndex], month, dateItems[dayIndex]);
return formatedDate;
}
This takes a date string and returns a formatted date. I then use that to do the comparision:
if (stringToDate(dateCompare, 'mm/did/yyyy', '/') >= stringToDate($scope.dateLimit, 'mm/did/yyyy', '/')) {
It works like a charm :)

Specified argument was out of the range of valid values. Parameter name: index

I have the following javascript code which must load a termset from sharepoint managed metadata store.
The code was working fine until monday and suddenly stopped working with the error on the title.
I changed the contents of the executequeryasync with just a console.log and I still have the error.
I already debugged line by line the execGetTermIds, and none of the lines there throwed the exception
function GetTermsDataFromTaxonomy(){
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStores = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStore = termStores.getByName("Taxonomy_kl5tZjInn7STsFTzIE7n3Q==");
//GUID of Term Set from which to get the Terms.
var termSet = termStore.getTermSet("31da4bc1-6429-499a-9d5e-be5e18b13c87");
var terms = termSet.getAllTerms();
var list;
var p = execGetTermIDs();
p.done(function(result) {
context.load(terms);
context.executeQueryAsync(function(){
console.log("hola");
},
function(sender,args){
console.log(args.get_message());
});
});
p.fail(function(result) {
// result is a string because that is what we passed to reject()!
var error = result;
console.log(error);
});
}
function sortTermsFromTree (tree) {
if (tree.children.length && tree.term.get_customSortOrder) {
var sortOrder = null;
if (tree.term.get_customSortOrder()) {
sortOrder = tree.term.get_customSortOrder();
}
// If not null, the custom sort order is a string of GUIDs, delimited by a :
if (sortOrder) {
sortOrder = sortOrder.split(':');
tree.children.sort(function (a, b) {
var indexA = sortOrder.indexOf(a.guid);
var indexB = sortOrder.indexOf(b.guid);
if (indexA > indexB) {
return 1;
} else if (indexA < indexB) {
return -1;
}
return 0;
});
}
// If null, terms are just sorted alphabetically
else {
tree.children.sort(function (a, b) {
if (a.title > b.title) {
return 1;
} else if (a.title < b.title) {
return -1;
}
return 0;
});
}
}
for (var i = 0; i < tree.children.length; i++) {
tree.children[i] = sortTermsFromTree(tree.children[i]);
}
return tree;
}
function execGetTermIDs(){
var d = $.Deferred();
var q = "<View><Query><Where></Where></Query></View>";
var context = new SP.ClientContext(siteUrl);
var oList = context.get_web().get_lists().getByTitle('TaxonomyHiddenList');
var camlQuery = SP.CamlQuery.createAllItemsQuery();
var collTermListItem = oList.getItems(camlQuery);
context.load(collTermListItem);
var o = {d: d, collTermListItem:collTermListItem};
context.executeQueryAsync(
Function.createDelegate(o, getTermIDsComplete),
Function.createDelegate(o, failCallback)
);
return d.promise();
}
function getTermIDsComplete()
{
var listItemEnumerator = this.collTermListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
termIDs[oListItem.get_fieldValues().Title] = oListItem.get_id();
}
this.d.resolve(termIDs);
}
function failCallback() {
this.d.reject("something bad happened");
}
omg, I found the error, and I cant find an explanation on this, hopefully somebody at Microsoft can tell me.
this line:
var termStore = termStores.getByName("Taxonomy_kl5tZjInn7STsFTzIE7n3Q==");
We noticed the name is now different, it has a different guid, but we as customers cant change that name, so the only reason is Microsoft changed it for us because it was working on monday.
Update: I changed that line with:
termStore = session.getDefaultSiteCollectionTermStore();
to avoid that it happens in the future.

Categories

Resources