javascript formatDate timezone displaying improperly - javascript

I'm trying to get this working off a branch of someone else's code, but wanted to take a hack at it, and so far, am not sure where the javascript is defined, etc. Not much knowledge here, but from what I'm understanding, this is suboptimal. I've tried a thing or two, but it breaks the whole page.
The TimeZone seems to display incorrectly: https://i.imgur.com/2OJrQuC.png
And the Javascript is here: https://github.com/mattgphoto/status/blob/master/script.js
It's something to do with formatDate, but am not sure what of the sections needs to be corrected, or if it's all of them. Would someone be able to help?

I think the following line in formatDate function is causing the problem.
return y.replace(/(y+)/g, function (v) {
return x.getFullYear().toString().slice(-v.length)
});
Try replacing that code with
return y;
Hope it works!

Related

Javascript: Array not shifting, multiple setTimeout functions (JSON)

I'm really stuck on this javascript question!
So I'm making a web page that will be completely animated (so it can be used for display for example in a television). That animation will be configurable by the user (stored in a database).
Right now, I've already made the code to store the configuration and to get the configuration (I do an AJAX call and save the configuration in an array of json objects) and everything is as it should be.
The problem is in the animation in which I go through the array and use setTimeout function to create animations. To iterate through the array I rotate it
(I use array.push(array.shift()) according to the answer here).
The first time the intervalmaster function is used, everything goes according to plan, but when the function is called again I need to rotate the array once more (because of the last animation) and the array just doesn't rotate!
Bellow I've left a portion of the code that I'm using that reproduces the problem I'm getting. I've also added the array jsonanima with some possible values (In reality the array is probably much bigger and with higher values).
I really don't understand what is happening, I've also considered that this could be a problem of the multiple setTimeout functions because I've read somewhere (couldn't find the link, sorry!) that is not really advised to use multiple setTimeout.
If that's the case is there any other way to do this?
Thank you in advance!
EDIT: Thanks to the comment from mplungjan I've realized that if change the console.log(jsonanimate) to console.log(JSON.stringfy(jsonanima)) it outputs the correct values (the json array rotated). This got me even more confused! Why do I need to JSON.stringfy to get the array in the correct order?!
Anyway, can't test this with the full code now as I'm not in the office, tomorrow I'll give more feedback. Thank you mplungjan.
EDIT2: Finally solved my problem! So the thing was the call to the function recursivegroup (recursivegroup(0);), this call was made before I rotated the array, so when restarting the animation the array would still have the incorrect values and every sub-sequential value was wrong.
A special thanks to mplungjan and trincot for the comments that helped me debug this problem.
Bellow I leave the code corrected so anybody with the same problem can check it out.
jsonanima=[{"VD":5,"A":10,"diff":0.25},{"L":7,"IE":8,"diff":0.25}];
function intervalmaster(flag){
function recursivegroup(index)
{
if(index==0)
{
//animateeach(jsonanima,0);
}
setTimeout(function(){
//HERE IT WORKS
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
//animateeach(jsonanima,0);
//removed the if statement, since it was irrelevant as mplungjan noted
recursivegroup(index+1);
},(jsonanima[0]['diff'])*60*1000);
}
//Changed this
//recursivegroup(0);
var mastertime=0;
for(var key in jsonanima)
{
mastertime+=(jsonanima[key]['diff']);
}
console.log(mastertime,flag);
console.log(JSON.stringify(jsonanima));
if(flag==true)
{
jsonanima.push(jsonanima.shift());
console.log(JSON.stringify(jsonanima));
}
//changed to here
recursivegroup(0);
masterinterval=setTimeout(function(){intervalmaster(true)},mastertime*60*1000);
}
intervalmaster(false);

Object has no method 'charAt' in jQuery plugin

I am attempting to use the autoNumeric jQuery plug-in which helps with the conversion of various currencies in jQuery.
The plug-in itself works when I use it in a jsFiddle example.
$(function () {
$('.money').autoNumeric('init', {
aSign: '$',
vMin: '-999999999.99',
nBracket: '(,)'
});
});
However, as soon as I integrate it into a big, legacy project, I start receiving the above error on line 194. I know why I'm getting the error - a string is not being passed into the negativeBracket function (negativeBracket(s, nBracket, oEvent) is the signature). Instead, it seems to be a jQuery object - e.fn.init1. I'm confused on how this might be happening. I realize the community may not be able to give a direct answer, but I would love (and will accept as an answer) being pointed in the right direction as nothing has jumped out at me so far.
Update
So, have some additional info that may be of help. It still has me stumped how it's happening (unfortunately, the answers below didn't help to provide any additional insight). When I link in autoNumeric, I key it off of any text field with the class money. It does work as I am typing in the box. I can see see formatting. However, when I tab into a new box, the box I just finished typing in clears itself completely after hitting line 152 in autoNumeric with the same exact error.
#Carlos487 was correct in his answer when he said I have an object that is not a string. I instead have an object that, I believe, is a function. Here's what I'm seeing in Chrome debugger tools:
e.fn.init[1]
> 0: input#price.money required
> context: input#price.money required
length: 1
selector: ""
> __proto__: Object[0]
The "arrowed" items can be further expanded out. I don't know if this provides any more clues, but it's at least something a bit different.
The errors like
no method XXXXX in Object
are produced because you are trying to call obj.XXXX() and obj is not of the desired type, in your particular case a string.
Have you tried in another browser because older or IE can be a little troublesome. I would recomend using chrome developer tools with your legacy app to see if anything else is conflicting or producing the error
I will bet money that you are using a second library which is interfering with jQuery. It has probably overridden $ with its own function.
Try using jQuery instead of $:
jQuery(function () {
jQuery('.money').autoNumeric('init', {
aSign: '$',
vMin: '-999999999.99',
nBracket: '(,)'
});
});
It turns out that the issue was a myriad of issue compounding into the error I saw. A couple things that was happening:
The validator plug-in was wrapping the jQuery object in its own structure (hence the charAt issue).
Once I fixed that, I also learned that some homegrown code was also wiping and rewriting data into the field to provide formatting (which is what autoNumeric is also doing), so autoNumeric would bomb out because it would get a null value and attempt to format it.
There was some other random craziness that also needed cleaned up. So...issue resolved! Still more to work on, but at least this hurdle is past. Thanks all for your help.

GAS Sort Multidimensional Array by Date

My script works sometimes and errors out sometimes. Running a debug shows that my array.sort may be the issue. This is the error:
We're sorry, a server error occurred. Please wait a bit and try again.
If it was the issue, I'm not sure why it would work sometimes and not others.
allData.sort(function(a,b){
a = new Date(a[7]);
b = new Date(b[7]);
return a>b?-1:a<b?1:0;
});
Is there anything wrong with that? The 7th spot of my array contains the date and I want to sort by that.
I believe you need to isolate your ternary operators.
return a>b?-1:(a<b?1:0);
For some reason, GAS seems to like this better:
allData.sort(function(a,b){return((a[7]<b[7])?-1:((a[7]>b[7])?1:0))});
Logger.log(allData)
allData.reverse()
Now it works. Not sure why, but that's the solution.

javascript localstorage

So, here's my code:
localStorage.setItem("nhac",for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor})
I'm so confused i can't think anymore xD, I've been trying all day long to make this work, i can't even explain it,sorry.
I have a popup which i opened from the main page. From the pop up window, im retrieving the elements from which i want to save the background-color so, by saving it in localstorage, it will always remain like that.
I've done others that work well, like this
localStorage.setItem("color_chosen2", opener.document.getElementById('header').style.color);
I don't know why it doesn't work, can anyone help me? It gives me some kind of error( the first one)
thanks
Edit:(sorry, I was in a hurry when i posted this)
The error it gives is "SyntaxError: syntax error"
(I'm using firefox, by the way)
Using the javascript console, this code
for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor}
returns the background color of the elements perfectly, but when I try to use it as the value in localStorage , it gives an error.
And no, there's no problem with the syntax localStorage.setItem, it's how it is, thanks.
Edit2: if you need to see how it works better, here's my website. the pop-up opens from the link in the bottom center.
You are trying to set a for loop into a setter which accepts a String. You must set each of the styles individually and with a unique key value.
Also, I don't think item is a method...
Try this...
items = opener.document.getElementsByName('link');
for ( p = 0; p < items.length; p++ ){
localStorage.setItem( p + 'nhac', items[p].style.backgroundColor );
}
Well, noone really effectively helped me, but at least 2 people tried to help, so thank you.
Anyway, i don't know how, I had an idea and the code works now...to anyone interested:
if(localStorage.getItem("color_chosen3")){
for(p=0;p<document.getElementsByName('link').length;p++){document.getElementsByName('link').item(p).style.backgroundColor=localStorage.getItem("color_chosen3")}}}
Problem solved, question closed...whatever. thanks

Execute function on webpage?

Say a webpage has a function like:
function abc (){
return 'abc';
}
How would I execute and receive the return value of that function from my extension? I've tried this, no dice:
var s = getBrowser.contentWindow.abc();
Thanks in advance!
I think that if you want to do this without major security holes, you need to be using XPCSafeJSObjectWrapper, but I'm having trouble finding good documentation on it. https://developer.mozilla.org/en/XPConnect_wrappers has a little bit. (It might be that it happens automatically when doing the above, but I'm not sure.)
I think I've solved my own problem with:
getBrowser().contentWindow.wrappedJSObject.funcFromPage();

Categories

Resources