Trying to make variable created inside function possible to access globally, so far
console.log(proper_head);
constantly displays "1" while I need it to be updated every time the function is executed, here is my function:
var i = 0;
function change_head() {
if (i < head_class.length) {
head.classList.add(head_class[i].name);
i++;
var h = i;
return h;
} else if (i = 3) {
head.className = "";
i -= 3;
var h = i;
return h;
}
}
var proper_head = change_head();
it is executed by pressing a button (not sure if it's important).
The only solution I came up with is to
setTimeout()
I'm sure there is a better way.
You could use a recursive implementation like this:
var i = 0;
function change_head() {
if (i < head_class.length) {
head.classList.add(head_class[i].name);
i++;
var h = i;
return h;
} else if (i = 3) {
head.className = "";
i -= 3;
var h = i;
return h;
}
change_head();
}
The problem is that your function is only being called once, you can call the function from within itself, however the above example will never stop, and will probably hit the max call stack size in your browser and then crash, i would recommend wrapping it in some sort of if statement so that it stops at some point, e.g i > 50.
Edit:
I see what your actual problem is, neither of your if blocks are firing, after the first press, head_class.length === 1 so "if (i < head_class.length)" wont fire, it's also not equal to 3 for the else block so nothing happens, the else block also has an error, try changing it to this:
if (i < 3) {
head.classList.add(head_class[i].name);
i++;
var h = i;
return h;
} else if (i == 3) {
head.className = "";
i -= 3;
var h = i;
return h;
}
i = 3 is for assignment, i == 3 is for comparison.
Hope this helps
Lloyd
Related
I may be overlooking something obvious but this just doesn't seem to go right.
I have this little code block in which an addEventListener method should trigger a function and meanwhile pass a parameter through so I can work with different functions while creating all EventListeners with one loop.
It does work, but the function gets invoked immediately when the site finishes loading.
Why doesn't the function wait until it gets triggered, but runs immediately?
var html = document.getElementsByTagName('html');
var pics = document.getElementsByClassName('pics');
var functions = [];
for(h=0;h<pics.length;h++){
array[h] = show(h);
}
for(p=0;p<pics.length;p++){
pics[p].addEventListener('click',array[p]);
}
function show(index){
html[0].style.backgroundColor = "black";
}
In the script
for (h = 0; h < pics.length; h++) {
array[h] = show(h);
}
you are calling the function show with value of h.
Its invoked at that time.So,background get setup with given color.
Its not because of addEventListener you are adding in the second for loop
You can write in this way.
for (h = 0; h < pics.length; h++) {
array[h] = show; // don't call,store the function reference
}
for (p = 0; p < pics.length; p++) {
pics[p].addEventListener('click', array[p].bind(this,p));
}
You can shorten two for loop into one
for (p = 0; p < pics.length; p++) {
pics[p].addEventListener('click', show.bind(this,p));
}
Note :
addEventListener expect callback and not an function call.
array[h] = show(h); is being invoked on page load. Here is working code.
var html = document.getElementsByTagName('html');
var pics = document.getElementsByClassName('pics');
var functions = [];
var array = [];
for(h=0;h<pics.length;h++){
array[h] = function () { show(h) };
}
for(p=0;p<pics.length;p++){
pics[p].addEventListener('click',array[p]);
}
function show(index){
html[0].style.backgroundColor = "black";
}
I have written a function that i want to detect the language of the text based on utf-8 encoding.Actualy this function determines the input argumant is english or not.The function work correctly in javascript console but when I use it in a loop ,the browser crashes.
//titles.lenght=>90
function is_eng(title) {
var A = 65;
var z = 122;
title = title.toString();
var eng_chars = 0;
var non_eng_chars = 0;
for (i = 0; i < title.length; i++) {
var c = title.charCodeAt(i);
if (c > A && c < z) {
eng_chars += 1;
} else {
non_eng_chars += 1;
}
}
if (eng_chars > non_eng_chars) {
return 1;
}
return 0;
}
You should add the keyword var before i=0, otherwise i is a global variable. If you use i for the external loop, you have an endless loop.
I'm trying do make a random post widget to work on my blog (blogger), but the browser keeps giving me an error saying that the script is unresponsive:
"A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.
Script: http://testeshoon.blogspot.pt/:4792"
The script came with the template.
Have tried other scripts from the web but they all do the same.
I'm trying to solve this for the past 2 days, but can't figure out what the problem is.
Can anyone help me?
Really need this to work, at least, in 2 widgets.
Tried scripts for recent posts and those work just fine, but the ones for random doesn't.
<ul id='random-posts'>
<script type='text/javaScript'>
var rdp_numposts = 2;
var rdp_snippet_length = 100;
var rdp_info = 'yes';
var rdp_comment = 'Comments';
var rdp_disable = 'Comments Disabled';
var rdp_current = [];
var rdp_total_posts = 0;
var rdp_current = new Array(rdp_numposts);
function totalposts(json) {
rdp_total_posts = json.feed.openSearch$totalResults.$t
}
document.write('<script type=\"text/javascript\" src=\"/feeds/posts/default/-/noticias?alt=json-in-script&max-results=0&callback=totalposts\"><\/script>');
function getvalue() {
for (var i = 0; i < rdp_numposts; i++) {
var found = false;
var rndValue = get_random();
for (var j = 0; j < rdp_current.length; j++) {
if (rdp_current[j] == rndValue) {
found = true;
break
}
};
if (found) {
i--
} else {
rdp_current[i] = rndValue
}
}
};
function get_random() {
var ranNum = 1 + Math.round(Math.random() * (rdp_total_posts - 1));
return ranNum
};
</script>
<script type='text/javaScript'>
var _0x3eb4=["\x65\x6E\x74\x72\x79","\x66\x65\x65\x64","\x24\x74","\x74\x69\x74\x6C\x65","\x63\x6F\x6E\x74\x65\x6E\x74","\x73\x75\x6D\x6D\x61\x72\x79","","\x72\x65\x70\x6C\x61\x63\x65","\x6C\x65\x6E\x67\x74\x68","\x73\x75\x62\x73\x74\x72\x69\x6E\x67","\x20","\x6C\x61\x73\x74\x49\x6E\x64\x65\x78\x4F\x66","\x26\x23\x31\x33\x33\x3B","\x6C\x69\x6E\x6B","\x74\x68\x72\x24\x74\x6F\x74\x61\x6C","\x72\x65\x6C","\x61\x6C\x74\x65\x72\x6E\x61\x74\x65","\x68\x72\x65\x66","\x70\x75\x62\x6C\x69\x73\x68\x65\x64","\x6D\x65\x64\x69\x61\x24\x74\x68\x75\x6D\x62\x6E\x61\x69\x6C","\x75\x72\x6C","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x2E\x62\x70\x2E\x62\x6C\x6F\x67\x73\x70\x6F\x74\x2E\x63\x6F\x6D\x2F\x2D\x35\x53\x6F\x56\x65\x31\x4B\x36\x4A\x53\x6B\x2F\x55\x74\x6C\x30\x4F\x4F\x6D\x75\x63\x41\x49\x2F\x41\x41\x41\x41\x41\x41\x41\x41\x46\x36\x45\x2F\x68\x51\x67\x68\x67\x44\x5F\x45\x4A\x64\x51\x2F\x73\x31\x36\x30\x30\x2F\x6E\x6F\x5F\x74\x68\x75\x6D\x62\x2E\x70\x6E\x67","\x3C\x6C\x69\x3E","\x77\x72\x69\x74\x65","\x3C\x61\x20\x68\x72\x65\x66\x3D\x22","\x22\x20\x72\x65\x6C\x3D\x22\x6E\x6F\x66\x6F\x6C\x6C\x6F\x77\x22\x3E\x3C\x69\x6D\x67\x20\x61\x6C\x74\x3D\x22","\x22\x20\x73\x72\x63\x3D\x22","\x22\x2F\x3E\x3C\x2F\x61\x3E","\x3C\x64\x69\x76\x3E\x3C\x61\x20\x68\x72\x65\x66\x3D\x22","\x22\x20\x72\x65\x6C\x3D\x22\x6E\x6F\x66\x6F\x6C\x6C\x6F\x77\x22\x20\x74\x69\x74\x6C\x65\x3D\x22","\x22\x3E","\x3C\x2F\x61\x3E\x3C\x2F\x64\x69\x76\x3E","\x79\x65\x73","\x3C\x73\x70\x61\x6E\x3E\x3C\x64\x69\x76\x20\x20\x63\x6C\x61\x73\x73\x3D\x22\x72\x70\x2D\x69\x6E\x66\x6F\x22\x3E","\x2F","\x20\x2D\x20","\x3C\x2F\x64\x69\x76\x3E\x3C\x2F\x73\x70\x61\x6E\x3E","\x3C\x62\x72\x2F\x3E\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x72\x70\x2D\x73\x6E\x69\x70\x70\x65\x74\x22\x3E","\x3C\x2F\x64\x69\x76\x3E\x3C\x64\x69\x76\x20\x73\x74\x79\x6C\x65\x3D\x22\x63\x6C\x65\x61\x72\x3A\x62\x6F\x74\x68\x22\x3E\x3C\x2F\x64\x69\x76\x3E\x3C\x2F\x6C\x69\x3E","\x3C\x73\x63\x72\x69\x70\x74\x20\x74\x79\x70\x65\x3D\x22\x74\x65\x78\x74\x2F\x6A\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x20\x73\x72\x63\x3D\x22\x2F\x66\x65\x65\x64\x73\x2F\x70\x6F\x73\x74\x73\x2F\x64\x65\x66\x61\x75\x6C\x74\x3F\x61\x6C\x74\x3D\x6A\x73\x6F\x6E\x2D\x69\x6E\x2D\x73\x63\x72\x69\x70\x74\x26\x73\x74\x61\x72\x74\x2D\x69\x6E\x64\x65\x78\x3D","\x26\x6D\x61\x78\x2D\x72\x65\x73\x75\x6C\x74\x73\x3D\x31\x26\x63\x61\x6C\x6C\x62\x61\x63\x6B\x3D\x72\x61\x6E\x64\x6F\x6D\x5F\x70\x6F\x73\x74\x73\x22\x3E\x3C\x2F\x73\x63\x72\x69\x70\x74\x3E"];function random_posts(_0x2f3cx2){for(var i=0;i<rdp_numposts;i++){var _0x2f3cx4=_0x2f3cx2[_0x3eb4[1]][_0x3eb4[0]][i];var _0x2f3cx5=_0x2f3cx4[_0x3eb4[3]][_0x3eb4[2]];if(_0x3eb4[4] in _0x2f3cx4){var _0x2f3cx6=_0x2f3cx4[_0x3eb4[4]][_0x3eb4[2]];} else {if(_0x3eb4[5] in _0x2f3cx4){var _0x2f3cx6=_0x2f3cx4[_0x3eb4[5]][_0x3eb4[2]];} else {var _0x2f3cx6=_0x3eb4[6];} ;} ;_0x2f3cx6=_0x2f3cx6[_0x3eb4[7]](/<[^>]*>/g,_0x3eb4[6]);if(_0x2f3cx6[_0x3eb4[8]]<rdp_snippet_length){var _0x2f3cx7=_0x2f3cx6;} else {_0x2f3cx6=_0x2f3cx6[_0x3eb4[9]](0,rdp_snippet_length);var _0x2f3cx8=_0x2f3cx6[_0x3eb4[11]](_0x3eb4[10]);_0x2f3cx7=_0x2f3cx6[_0x3eb4[9]](0,_0x2f3cx8)+_0x3eb4[12];} ;for(var _0x2f3cx9=0;_0x2f3cx9<_0x2f3cx4[_0x3eb4[13]][_0x3eb4[8]];_0x2f3cx9++){if(_0x3eb4[14] in _0x2f3cx4){var _0x2f3cxa=_0x2f3cx4[_0x3eb4[14]][_0x3eb4[2]]+_0x3eb4[10]+rdp_comment;} else {_0x2f3cxa=rdp_disable;} ;if(_0x2f3cx4[_0x3eb4[13]][_0x2f3cx9][_0x3eb4[15]]==_0x3eb4[16]){var _0x2f3cxb=_0x2f3cx4[_0x3eb4[13]][_0x2f3cx9][_0x3eb4[17]];var _0x2f3cxc=_0x2f3cx4[_0x3eb4[18]][_0x3eb4[2]];if(_0x3eb4[19] in _0x2f3cx4){var _0x2f3cxd=_0x2f3cx4[_0x3eb4[19]][_0x3eb4[20]];} else {_0x2f3cxd=_0x3eb4[21];} ;} ;} ;document[_0x3eb4[23]](_0x3eb4[22]);document[_0x3eb4[23]](_0x3eb4[24]+_0x2f3cxb+_0x3eb4[25]+_0x2f3cx5+_0x3eb4[26]+_0x2f3cxd+_0x3eb4[27]);document[_0x3eb4[23]](_0x3eb4[28]+_0x2f3cxb+_0x3eb4[29]+_0x2f3cx7+_0x3eb4[30]+_0x2f3cx5+_0x3eb4[31]);if(rdp_info==_0x3eb4[32]){document[_0x3eb4[23]](_0x3eb4[33]+_0x2f3cxc[_0x3eb4[9]](8,10)+_0x3eb4[34]+_0x2f3cxc[_0x3eb4[9]](5,7)+_0x3eb4[34]+_0x2f3cxc[_0x3eb4[9]](0,4)+_0x3eb4[35]+_0x2f3cxa)+_0x3eb4[36];} ;document[_0x3eb4[23]](_0x3eb4[37]+_0x2f3cx7+_0x3eb4[38]);} ;} ;getvalue();for(var i=0;i<rdp_numposts;i++){document[_0x3eb4[23]](_0x3eb4[39]+rdp_current[i]+_0x3eb4[40]);} ;
</script>
</ul>
Let's suppose that you want k distinct numbers (random post indexes) from n possible numbers. Here is an implementation
function getRandomSubset(n, k) {
if (k > n) {
//impossible
return;
}
//initializing the result set as empty
var output = [];
//creating a variable
var input;
//we need k distinct random numbers
while (k-- > 0) {
//we get a random integer, 0 <= input < n
input = Math.floor(Math.random() * n);
//if it is duplicated, increment it inside the n'th modulo class until a new value is found
while (output.indexOf(input) >= 0) {
input = (input + 1) % n;
}
//store the new distinct value into the results
output[output.length] = input;
}
return output;
}
There are many other possible ways.
I've got a very similar process that occurs in many places, with one small change. I was wondering what would be the best way to make the code neater. Currently, the process is:
var drawx = 0;
var drawy = 0;
while(drawy < 22){
while(drawx < 10){
if(nextBlock[drawx][drawy] != false){
base[drawx][drawy] = nextBlock[drawx][drawy];
}
drawx++;
}
drawx = 0;
drawy++;
}
Within the while drawx < 10 is where I usually have different things to run, and I'm not sure how I would create a function with a variable process in the middle of it. Is there a way to do this, or should I just create a function that does this process and executes a certain if statement depending on the parameter that was called when running the function?
EDIT: I think I might have not gotten my initial problem across. I want to be able to have a process such as the if statement within the loop to be a variable process while the rest of it to be the same
javascript has first class functions so you can do something like this:
function drawLoop(action) {
var drawx = 0;
var drawy = 0;
while(drawy < 22){
while(drawx < 10){
action(drawx, drawy);
drawx++;
}
drawx = 0;
drawy++;
}
}
And then call it like this:
drawLoop(function(drawx, drawy){
if(nextBlock[drawx][drawy] != false){
base[drawx][drawy] = nextBlock[drawx][drawy];
}
});
And of course, you can change the function you pass to drawLoop to suite your needs.
you could inject a function as a variable to your generic function like
function genericDraw(xValidation) {
var drawx = 0;
var drawy = 0;
while(drawy < 22){
while(xValidation(drawX)){
if(nextBlock[drawx][drawy] != false){
base[drawx][drawy] = nextBlock[drawx][drawy];
}
drawx++;
}
drawx = 0;
drawy++;
}
}
where xValidation is
function someValidation(value) {
return (value < 10);
}
and you call it
genericDraw(someValidation);
for( var drawy = 0 ; drawy < 22 ; drawy++) {
for( var drawx = 0 ; drawx < 10 ; drawx ++) {
if(nextBlock[drawx][drawy] != false) {
base[drawx][drawy] = nextBlock[drawx][drawy];
}
}
}
I've written a function that contains an if statement and the condition contains a variable.
function interval() {
var interval = setInterval(function() {
if (x < max) {
x = parseInt(x) + 1;
$('.max').html(addCommas(x));
}
}, 1);
};
So here max is my variable. Works great, but now I have three more variables and I want to run the same function but with the different variables each time. I could copy the function 3 more times and edit the variable in each one but that strikes me as highly inefficient.
What would be the best way to achieve this Stacked?
Pass max as an argument to the function
function interval(max){
var x = 0; //always declare variables
var interval = setInterval( function(){
if (x < max){
x = parseInt(x) + 1;
$('.max').html(addCommas(x));
}
}, 1);
};
I'm not sure of the driver for putting this code in an interval. You may want to consider using a for loop:
function interval(max){
for(var x = 0; x < max; x++){
$(".max").html(addCommas(x));
}
};
Use the parameters in function and use same function.
function interval(vfield){
var interval = setInterval( function(){
if (x < vfield){
x = parseInt(x) + 1;
$('.max').html(addCommas(x));
}
}, 1);
};
Where vfield is your variable.