I am using 2 JSON feeds to check the date and display the data from them depending on the current date and the date in one of the objects, but for some reason variable c which is object is undefined. When I replace the data in 'elements' function with hardcoded urls, everything works fine, but I am not sure why data isn't stored inside the c object:
jQuery(function ($) {
var url1 = 'feed1.json';
var url2 = 'feed2.json';
var id = shop_id.replace(/\[|\]|\"/g, '');
var c = {};
var logo;
$.when(request1(), request2()).done(function (r1, r2) {
var results1 = $.grep(r1[0], function (e) {return e.id == id});
var results2 = $.grep(r2[0], function (e) {return e.shop_id == id});
var fallback = $.grep(r2[0], function (e) {return e.PSN == 'fallback160'});
if (!$.isEmptyObject(results2)) {
if (!$.isEmptyObject(results1)) {
var today = new Date();
var endDate = formatDate(results1[0].Ende);
var startDate = formatDate(results1[0].Start);
console.log(endDate);
console.log(startDate);
if (today <= endDate && today >= startDate) {
c = {'one': results1[0].INC_Suffix, 'separator': ' bis ', 'two': results1[0].Ende, 'link': results1[0].Deeplink, 'logo': results2[0].logo_url};
elements();
}
}
else {
c = {'one': results2[0].STD_INC_Factor, 'separator': ' ', 'two': results2[0].STD_INC_Suffix, 'link': results2[0].deeplink, 'logo': results2[0].logo_url};
elements();
}
}
else {
$('#clicktag').html('<img src="' + fallback[0].logo_url + '">').attr('href', clicktag + encodeURIComponent(fallback[0].deeplink));
}
//resize fonts based on height of the container
var intBoxHeight = $('#interupter').height();
var intInnerHeight = $('#interupterInner').height();
var intFontSize = parseInt($('#interupterInner').css('font-size'));
while (intInnerHeight > intBoxHeight) {
--intFontSize;
$('#interupterInner').css('font-size', intFontSize + 'px');
intBoxHeight = $('#interupter').height();
intInnerHeight = $('#interupterInner').height();
}
}).fail(function () {
c = {'one': 'DIE BESTEN', 'separator': ' ', 'two': 'ANGEBOTE', 'link': '#', 'logo': 'img/fallback.png'};
elements();
})
function elements () {
$('#storeLogo span').html('<img src=\'' + c.logo + '\'>');
$('#interupterInner').html(c.one + c.separator + c.two);
$('#clicktag').attr('href', clicktag + encodeURIComponent(c.link));
tl.play();
}
function formatDate (d) {
var part = d.split('.');
return new Date(part[1] + '.' + part[0] + '.' + part[2]);
}
console.log(elements());
function request1 () {return $.getJSON(url1)};
function request2 () {return $.getJSON(url2)};
})
ca value. My advice would be to step through with your browser's JavaScript debugger