I have run in to a stupid problem...
I declared a new variable called leadingZero. I save the modified .js file and run the project with a breakpoint on the leadingZero assignment and in watcher window it says its undefined after passing this line, but all the other declarations here are working fine and I can see the assigned values. needless to say the getObject call does not work now.
var leadingZero = 0; //new variable
var chkActive;
var chkSubscribe;
var hdnItem = getObject('hdnItemCounter');
var ItemCount = parseInt(hdnItem.value) + 1;
for (intCounter = 2; intCounter <= ItemCount; intCounter++) {
chkActive = getObject('dgrProductList_ctl0' + leadingZero + intCounter + '_chkActive');
}
Check this http://jsfiddle.net/DHDsE/
Not getting the undefined problem tho, but having to add toString() to leadingZero for it to render in the console.log, so maybe that's your issue too.
You did set the breakpoint on the line below, didn't you?
Because if you set it on the line var leadingZero = 0; it halts before the line is evaluated, which explains the undefined value in the watcher.
Also, as gillesc pointed out, your leadingZero must be a string, otherwise you're adding up intCounter and leadingZero, rather than concatenating them.
The problem seemed to be changes to the js were not loaded in ie cache. even after closing ie, rebuilding the project and running again, I still need to hit ctrl+f5 on the page to load the new javascript
Related
I have a block of code that uses Math.random(). I noticed that occasionally the return value would be "Undefined". This is the code I used:
return data.map(val => {
var r = Math.random();
if (r < this.mutChance) {
console.log(Math.random);
debugger;
return this.rDna(val);
}
return val;
});
When i set the mutChance variable to 0 and let the code run for a bit eventually debugger gets called and shows the value of r to be undefined. I tried to reproduce the problem by running in console
var test = Math.random();
while(test){
test = Math.random();
}
However, this loop never ended. I have no idea why the function would act differently within my object, and the console.log(Math.random); Says that the function still has its native code. Nowhere do I override the random function, nor do I use the variable r anywhere else.
I am relatively new to JavaScript and couldn't find this problem anywhere else. The only other code I have imported is the p5.min.js package.
Problem was with how chrome was interpreting the very small values
Without console.log chrome shows it like this
With console.log chrome displays correctly
This is so simple I forgot how to do it. I've always passed variables to a function hence it's param's were pre-set, now I need to set the param's when declaring the function, but don't remember the setup.
I'm looking for the working version of this:
function(a,b=4){return a-b;}
Where the b param' of the function is set when the function is declared.
If I remember rightly it's like setting a default for b if the function has no second argument:
function(a,b){b=b || 4; return a-b;}
EDIT
Thanks for all your help but it seems it's impossible in js without ECMAScript 6. Your answers are getting a bit off topic though... I really needed the values set in the paren's.
To keep it on topic... my initial problem is sending parameters to a setTimeout function. Ok so I have a <div> with a .gif background, when clicked it's background changes, this second animation runs for exactly 8 seconds and then the background changes again to a final .gif. so it's a 3 stage animation, simple... thing is the 8sec gap, I figured a setTimeout would work but I can't pass any param's to the 'sto' function to reference said <div>.
If you know of any timer events that can help then be my guest, this is as far as I've got. My original code is below... it fails at function(p = cha).
for(var i = 0; i < 5; i++){
var cha = document.createElement('div');
$(cha).css('background','url(img/stand.gif)');
cha.addEventListener('click',function(){
$(cha).css('background','url(img/walk.gif)');
setTimeout(function(p = cha){
$(p).css('background','url(img/walk.gif)');
},8000);
});
}
function(a,b){b=b || 4; return a-b;}
This is the typical way to default params in ES5. However I would advise changing this to check b's typs a little more strictly, because 0 will be considered a falsey value by the || operator
function(a,b){
b = typeof b === 'undefined' ? 4 : b;
return a-b;
}
I am trying to do something really simple - initialize an array in Javascript. And it's not working in Google Chrome. Here is the code:
status = [];
for(i=0; i < 8; i++)
status[i]=false;
alert(status.length); //It says 0 when it should say 8
What gives?
The assignment of your status variable, clashes with the window.status property.
Chrome simply refuses to make the assignment.
The window.status property, sets or gets the text in the status bar at the bottom of the browser.
I would recommend you to either, rename your variable or use an anonymous function to create a new scope, also remember to always use var for declaring variables:
(function () {
var status = [];
for (var i = 0; i < 8; i++)
status[i] = false;
alert(status.length);
})();
Change the variable name. Seems like status is a property of window, and Chrome makes it inmutable
.
I didn't expect that, too.
The problem here is what status is attached to. You are using it off the global/window scope.
Back in the good ole days we were able to set the text in the status bar. How you would do it is by setting window.status to a string value. So what you are doing is NOT setting a variable, but changing the string of the browser's status bar.
I'm trying to create a page that allows for the multiple upload of images, this requires different name attributes. To achieve this, I'm using JS to add one the variable i giving a number.
The below code returns NaN, I'm not too sure why?
$('document').ready(function() {
var i = 1;
$('#new-dialogue').click(function() {
var i = i + 1;
$('.create-upload').append('<div class="upload"><input type="file" name="image' + i + '"/></div>');
});
});
Remove the second var.
What your current code is saying, is what when new-dialogue is clicked, it should create a variable called i and set it to i+1... but because i hasn't been defined yet in this scope you are doing undefined + 1, which is NaN.
Removing the second var will cause the click function to get the i variable from the containing scope, which is what you want it to do. You can then just have i++ to increment it as needed.
That said, you could just make your life easier by using:
<input type="file" name="image[]" />
Because on the server side, you will then have an array of uploaded files ;)
Instead of
var i = i + 1;
Just do
i++;
You need to increment already declared variable, not re-declare it again.
When you redeclared i local to the callback, your function got its own local copy of i that had yet to receive a value. So var i = i + 1; is basically var i = undefined + 1;, which evaluates to NaN.
Get rid of the var to fix this.
I am trying to do something really simple - initialize an array in Javascript. And it's not working in Google Chrome. Here is the code:
status = [];
for(i=0; i < 8; i++)
status[i]=false;
alert(status.length); //It says 0 when it should say 8
What gives?
The assignment of your status variable, clashes with the window.status property.
Chrome simply refuses to make the assignment.
The window.status property, sets or gets the text in the status bar at the bottom of the browser.
I would recommend you to either, rename your variable or use an anonymous function to create a new scope, also remember to always use var for declaring variables:
(function () {
var status = [];
for (var i = 0; i < 8; i++)
status[i] = false;
alert(status.length);
})();
Change the variable name. Seems like status is a property of window, and Chrome makes it inmutable
.
I didn't expect that, too.
The problem here is what status is attached to. You are using it off the global/window scope.
Back in the good ole days we were able to set the text in the status bar. How you would do it is by setting window.status to a string value. So what you are doing is NOT setting a variable, but changing the string of the browser's status bar.