code is working while debug, but without not - Javascript / Jquery











up vote
0
down vote

favorite












I have an issue that I don't understand and I have no idea how to fix it, where to look for the cause. When I'm debugin (chrome) every thing is working, but during normal use it dosen't. For me is some kind of Science-Fiction, it would be better for me if it's more Science than Fiction :)



for (var i = 0; i < filteredAddedFiles.length; i++) {
if ((/.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name)) {

(function (file) {
var reader = new FileReader();
var blob = b64toBlob(file.base64.replace('data:image/jpeg;base64,', ''), file.type);
reader.addEventListener("load", function () {

console.log(this);
var image = new Image();
image.src = window.URL.createObjectURL(blob);
image.onload = function () {
preview.innerHTML += drawHtml(image, file)
};

//I tried:
//(function (b) {
// var image = new Image();
// image.addEventListener("load", function () {
// preview.innerHTML += drawHtml(this, file);
// //window.URL.revokeObjectURL(image.src);
// });
// image.src = window.URL.createObjectURL(b);
//})(blob);
});
reader.readAsDataURL(blob);
})(filteredAddedFiles[i]);

} else {
errors += filteredAddedFiles[i].name + " Unsupported Image extensionn";
}
}


here I attached a short movie that shows how its working
link to movie



not working - I mean - it looks like the all thing inside for dosen't executed



EDIT: 1



@Teemu - I turn on logs in chrome console and all console.log's appear



@Katie.Sun - now the above for - console.log(filteredAddedFiles.length); is 0 - but when I'm debuging code the same console.log(filteredAddedFiles.length); have values !



EDIT: 2



@Matti Price
filteredAddedFiles - is the result of custom logic of page, filtering,
validation etc.
Everything starts here:



addedFiles = added(files); // files - FileList from input this is a read only array of obj

function added(from) {
var out = ;
for (var i = 0; i < from.length; i++) {
(function (obj) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
out.push(row);
});
readerBase64.readAsDataURL(obj);
})(from[i]);
}
return out;
}


then addedFiles - do something farther and transform into filteredAddedFiles later. what I found in added function? during debug there is an length value witch is correct, but when I opened the __proto__: Array(0) I found length property = 0.



Should this length value be equal to the value from above length?



print screen



The second thing:
I have to admit that I don't have enough knowledge aboute addEventListener. Are there any queues here or some thread etc?



EDIT: 3
After last @Teemu comment I made some changes (I had to read a lot aboute promisses etc:)), but output is the same console.log("out resolve", out); shows a array of object, but console.log("out.length then", out.length); shows 0 and the small blue i-icon show msg - Value below evaluated just now



var out = ;
for (var i = 0; i < files.length; i++) {
fillArray(files[i], out);
}
console.log("out resolve", out);
console.log("out.length then", out.length);

function fillArray(obj, out) {
return new Promise(function (resolve, reject) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, out.push(row);
resolve(out);
});
readerBase64.readAsDataURL(obj);
});
}


After I posted the edit above I relized that I just create promise, I forgot to call `promise



, but this is not important, because 90% of my code has been changed because of this topic and the answer of the Golden Person @Kaiido



URL.createObjectURL() - is synchronous. You don't need your Promise wrapping event handlers hell, all can be done in a single loop.



In my case, this is a better solution than a filereader, I have to upload only images, and with some restrictions thanks to which I don't have to worry about freeze the Internet browser, because of the synchronous nature of createObjectURL()
Thank you for your help and commitment










share|improve this question
























  • You should write down what is not working. Are there any errors in the console?
    – Andy
    Nov 8 at 19:16






  • 1




    Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
    – Teemu
    Nov 8 at 19:23






  • 1




    @szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
    – Katie.Sun
    Nov 8 at 19:34






  • 1




    Make sure the logs are shown, "Default" should be selected from "Log levels".
    – Teemu
    Nov 8 at 19:53








  • 1




    Where is filteredAddedFiles coming from?
    – Matti Price
    Nov 8 at 21:53















up vote
0
down vote

favorite












I have an issue that I don't understand and I have no idea how to fix it, where to look for the cause. When I'm debugin (chrome) every thing is working, but during normal use it dosen't. For me is some kind of Science-Fiction, it would be better for me if it's more Science than Fiction :)



for (var i = 0; i < filteredAddedFiles.length; i++) {
if ((/.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name)) {

(function (file) {
var reader = new FileReader();
var blob = b64toBlob(file.base64.replace('data:image/jpeg;base64,', ''), file.type);
reader.addEventListener("load", function () {

console.log(this);
var image = new Image();
image.src = window.URL.createObjectURL(blob);
image.onload = function () {
preview.innerHTML += drawHtml(image, file)
};

//I tried:
//(function (b) {
// var image = new Image();
// image.addEventListener("load", function () {
// preview.innerHTML += drawHtml(this, file);
// //window.URL.revokeObjectURL(image.src);
// });
// image.src = window.URL.createObjectURL(b);
//})(blob);
});
reader.readAsDataURL(blob);
})(filteredAddedFiles[i]);

} else {
errors += filteredAddedFiles[i].name + " Unsupported Image extensionn";
}
}


here I attached a short movie that shows how its working
link to movie



not working - I mean - it looks like the all thing inside for dosen't executed



EDIT: 1



@Teemu - I turn on logs in chrome console and all console.log's appear



@Katie.Sun - now the above for - console.log(filteredAddedFiles.length); is 0 - but when I'm debuging code the same console.log(filteredAddedFiles.length); have values !



EDIT: 2



@Matti Price
filteredAddedFiles - is the result of custom logic of page, filtering,
validation etc.
Everything starts here:



addedFiles = added(files); // files - FileList from input this is a read only array of obj

function added(from) {
var out = ;
for (var i = 0; i < from.length; i++) {
(function (obj) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
out.push(row);
});
readerBase64.readAsDataURL(obj);
})(from[i]);
}
return out;
}


then addedFiles - do something farther and transform into filteredAddedFiles later. what I found in added function? during debug there is an length value witch is correct, but when I opened the __proto__: Array(0) I found length property = 0.



Should this length value be equal to the value from above length?



print screen



The second thing:
I have to admit that I don't have enough knowledge aboute addEventListener. Are there any queues here or some thread etc?



EDIT: 3
After last @Teemu comment I made some changes (I had to read a lot aboute promisses etc:)), but output is the same console.log("out resolve", out); shows a array of object, but console.log("out.length then", out.length); shows 0 and the small blue i-icon show msg - Value below evaluated just now



var out = ;
for (var i = 0; i < files.length; i++) {
fillArray(files[i], out);
}
console.log("out resolve", out);
console.log("out.length then", out.length);

function fillArray(obj, out) {
return new Promise(function (resolve, reject) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, out.push(row);
resolve(out);
});
readerBase64.readAsDataURL(obj);
});
}


After I posted the edit above I relized that I just create promise, I forgot to call `promise



, but this is not important, because 90% of my code has been changed because of this topic and the answer of the Golden Person @Kaiido



URL.createObjectURL() - is synchronous. You don't need your Promise wrapping event handlers hell, all can be done in a single loop.



In my case, this is a better solution than a filereader, I have to upload only images, and with some restrictions thanks to which I don't have to worry about freeze the Internet browser, because of the synchronous nature of createObjectURL()
Thank you for your help and commitment










share|improve this question
























  • You should write down what is not working. Are there any errors in the console?
    – Andy
    Nov 8 at 19:16






  • 1




    Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
    – Teemu
    Nov 8 at 19:23






  • 1




    @szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
    – Katie.Sun
    Nov 8 at 19:34






  • 1




    Make sure the logs are shown, "Default" should be selected from "Log levels".
    – Teemu
    Nov 8 at 19:53








  • 1




    Where is filteredAddedFiles coming from?
    – Matti Price
    Nov 8 at 21:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an issue that I don't understand and I have no idea how to fix it, where to look for the cause. When I'm debugin (chrome) every thing is working, but during normal use it dosen't. For me is some kind of Science-Fiction, it would be better for me if it's more Science than Fiction :)



for (var i = 0; i < filteredAddedFiles.length; i++) {
if ((/.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name)) {

(function (file) {
var reader = new FileReader();
var blob = b64toBlob(file.base64.replace('data:image/jpeg;base64,', ''), file.type);
reader.addEventListener("load", function () {

console.log(this);
var image = new Image();
image.src = window.URL.createObjectURL(blob);
image.onload = function () {
preview.innerHTML += drawHtml(image, file)
};

//I tried:
//(function (b) {
// var image = new Image();
// image.addEventListener("load", function () {
// preview.innerHTML += drawHtml(this, file);
// //window.URL.revokeObjectURL(image.src);
// });
// image.src = window.URL.createObjectURL(b);
//})(blob);
});
reader.readAsDataURL(blob);
})(filteredAddedFiles[i]);

} else {
errors += filteredAddedFiles[i].name + " Unsupported Image extensionn";
}
}


here I attached a short movie that shows how its working
link to movie



not working - I mean - it looks like the all thing inside for dosen't executed



EDIT: 1



@Teemu - I turn on logs in chrome console and all console.log's appear



@Katie.Sun - now the above for - console.log(filteredAddedFiles.length); is 0 - but when I'm debuging code the same console.log(filteredAddedFiles.length); have values !



EDIT: 2



@Matti Price
filteredAddedFiles - is the result of custom logic of page, filtering,
validation etc.
Everything starts here:



addedFiles = added(files); // files - FileList from input this is a read only array of obj

function added(from) {
var out = ;
for (var i = 0; i < from.length; i++) {
(function (obj) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
out.push(row);
});
readerBase64.readAsDataURL(obj);
})(from[i]);
}
return out;
}


then addedFiles - do something farther and transform into filteredAddedFiles later. what I found in added function? during debug there is an length value witch is correct, but when I opened the __proto__: Array(0) I found length property = 0.



Should this length value be equal to the value from above length?



print screen



The second thing:
I have to admit that I don't have enough knowledge aboute addEventListener. Are there any queues here or some thread etc?



EDIT: 3
After last @Teemu comment I made some changes (I had to read a lot aboute promisses etc:)), but output is the same console.log("out resolve", out); shows a array of object, but console.log("out.length then", out.length); shows 0 and the small blue i-icon show msg - Value below evaluated just now



var out = ;
for (var i = 0; i < files.length; i++) {
fillArray(files[i], out);
}
console.log("out resolve", out);
console.log("out.length then", out.length);

function fillArray(obj, out) {
return new Promise(function (resolve, reject) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, out.push(row);
resolve(out);
});
readerBase64.readAsDataURL(obj);
});
}


After I posted the edit above I relized that I just create promise, I forgot to call `promise



, but this is not important, because 90% of my code has been changed because of this topic and the answer of the Golden Person @Kaiido



URL.createObjectURL() - is synchronous. You don't need your Promise wrapping event handlers hell, all can be done in a single loop.



In my case, this is a better solution than a filereader, I have to upload only images, and with some restrictions thanks to which I don't have to worry about freeze the Internet browser, because of the synchronous nature of createObjectURL()
Thank you for your help and commitment










share|improve this question















I have an issue that I don't understand and I have no idea how to fix it, where to look for the cause. When I'm debugin (chrome) every thing is working, but during normal use it dosen't. For me is some kind of Science-Fiction, it would be better for me if it's more Science than Fiction :)



for (var i = 0; i < filteredAddedFiles.length; i++) {
if ((/.(png|jpeg|jpg|gif)$/i).test(filteredAddedFiles[i].name)) {

(function (file) {
var reader = new FileReader();
var blob = b64toBlob(file.base64.replace('data:image/jpeg;base64,', ''), file.type);
reader.addEventListener("load", function () {

console.log(this);
var image = new Image();
image.src = window.URL.createObjectURL(blob);
image.onload = function () {
preview.innerHTML += drawHtml(image, file)
};

//I tried:
//(function (b) {
// var image = new Image();
// image.addEventListener("load", function () {
// preview.innerHTML += drawHtml(this, file);
// //window.URL.revokeObjectURL(image.src);
// });
// image.src = window.URL.createObjectURL(b);
//})(blob);
});
reader.readAsDataURL(blob);
})(filteredAddedFiles[i]);

} else {
errors += filteredAddedFiles[i].name + " Unsupported Image extensionn";
}
}


here I attached a short movie that shows how its working
link to movie



not working - I mean - it looks like the all thing inside for dosen't executed



EDIT: 1



@Teemu - I turn on logs in chrome console and all console.log's appear



@Katie.Sun - now the above for - console.log(filteredAddedFiles.length); is 0 - but when I'm debuging code the same console.log(filteredAddedFiles.length); have values !



EDIT: 2



@Matti Price
filteredAddedFiles - is the result of custom logic of page, filtering,
validation etc.
Everything starts here:



addedFiles = added(files); // files - FileList from input this is a read only array of obj

function added(from) {
var out = ;
for (var i = 0; i < from.length; i++) {
(function (obj) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
out.push(row);
});
readerBase64.readAsDataURL(obj);
})(from[i]);
}
return out;
}


then addedFiles - do something farther and transform into filteredAddedFiles later. what I found in added function? during debug there is an length value witch is correct, but when I opened the __proto__: Array(0) I found length property = 0.



Should this length value be equal to the value from above length?



print screen



The second thing:
I have to admit that I don't have enough knowledge aboute addEventListener. Are there any queues here or some thread etc?



EDIT: 3
After last @Teemu comment I made some changes (I had to read a lot aboute promisses etc:)), but output is the same console.log("out resolve", out); shows a array of object, but console.log("out.length then", out.length); shows 0 and the small blue i-icon show msg - Value below evaluated just now



var out = ;
for (var i = 0; i < files.length; i++) {
fillArray(files[i], out);
}
console.log("out resolve", out);
console.log("out.length then", out.length);

function fillArray(obj, out) {
return new Promise(function (resolve, reject) {
var readerBase64 = new FileReader();
readerBase64.addEventListener("load", function () {
var fileBase64 = readerBase64.result;
var row = { name: obj.name, size: obj.size, type: obj.type, out.push(row);
resolve(out);
});
readerBase64.readAsDataURL(obj);
});
}


After I posted the edit above I relized that I just create promise, I forgot to call `promise



, but this is not important, because 90% of my code has been changed because of this topic and the answer of the Golden Person @Kaiido



URL.createObjectURL() - is synchronous. You don't need your Promise wrapping event handlers hell, all can be done in a single loop.



In my case, this is a better solution than a filereader, I have to upload only images, and with some restrictions thanks to which I don't have to worry about freeze the Internet browser, because of the synchronous nature of createObjectURL()
Thank you for your help and commitment







javascript jquery google-chrome debugging






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:20

























asked Nov 8 at 19:14









szkut

7810




7810












  • You should write down what is not working. Are there any errors in the console?
    – Andy
    Nov 8 at 19:16






  • 1




    Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
    – Teemu
    Nov 8 at 19:23






  • 1




    @szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
    – Katie.Sun
    Nov 8 at 19:34






  • 1




    Make sure the logs are shown, "Default" should be selected from "Log levels".
    – Teemu
    Nov 8 at 19:53








  • 1




    Where is filteredAddedFiles coming from?
    – Matti Price
    Nov 8 at 21:53


















  • You should write down what is not working. Are there any errors in the console?
    – Andy
    Nov 8 at 19:16






  • 1




    Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
    – Teemu
    Nov 8 at 19:23






  • 1




    @szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
    – Katie.Sun
    Nov 8 at 19:34






  • 1




    Make sure the logs are shown, "Default" should be selected from "Log levels".
    – Teemu
    Nov 8 at 19:53








  • 1




    Where is filteredAddedFiles coming from?
    – Matti Price
    Nov 8 at 21:53
















You should write down what is not working. Are there any errors in the console?
– Andy
Nov 8 at 19:16




You should write down what is not working. Are there any errors in the console?
– Andy
Nov 8 at 19:16




1




1




Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
– Teemu
Nov 8 at 19:23




Then the first thing to do is to check the value of filteredAddedFiles.length before the loop.
– Teemu
Nov 8 at 19:23




1




1




@szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
– Katie.Sun
Nov 8 at 19:34




@szkut how are you calling this part of your code? if you just do a console.log("here"); before the for loop and run it normally, what happens? Because if the answer is nothing then whatever is causing this problem is not in the block of code you posted
– Katie.Sun
Nov 8 at 19:34




1




1




Make sure the logs are shown, "Default" should be selected from "Log levels".
– Teemu
Nov 8 at 19:53






Make sure the logs are shown, "Default" should be selected from "Log levels".
– Teemu
Nov 8 at 19:53






1




1




Where is filteredAddedFiles coming from?
– Matti Price
Nov 8 at 21:53




Where is filteredAddedFiles coming from?
– Matti Price
Nov 8 at 21:53

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214645%2fcode-is-working-while-debug-but-without-not-javascript-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214645%2fcode-is-working-while-debug-but-without-not-javascript-jquery%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values