chrome.storage.sync.get external variable not visible in function - google-chrome

i have this simple code for my chrome extension:
ar = ["a", "e", "i"];
for (k=0; k<2; k++) {
var ids= ar[k];
chrome.storage.sync.get('info_Email', hello);
function hello (data){
console.log(data); //it works
console.log('ids: '+ ids); // undefined
}
}
"info_Email" is stored ok, but variable "ids" always display last value "i" inside function, why is this happening?

Related

variable insert to a blink function

I'm new to HTML and ajax. I'm trying to insert a ip list from flask , to the ajax and trigger the js function to blink.
but somehow I can't find a way to insert the ip variable (response[i]) into the function value column in a right way.
it is to trigger the blink on the required ip tab in html.
function ajaxForm(){
// var form= new FormData(document.getElementById("myform2"));
var data = {"name":"John Doe"}
$.ajax({
url:"{{ url_for('Submit_form') }}",
type:"post",
contentType:'application/json',
data:JSON.stringify(data),
dataType: "json",
processData:false,
// async: false
success:function(response){
// alert(response)
if (response == "success")
{alert("Success !!!" );}
else {
for(i in response)
{
BLINK(response[i]);
}
}
},
// #time out 也进入 error
error:function(e){
// alert(e.)
alert("Failed submit form trigger!!!!");
}
})
}
<script type="text/javascript">
function BLINK(){
var t = null;
function blink() {
var obj = $('input[id="IP"][value=response[i]]') . <---- here
obj.addClass("blink-class");
t = setTimeout(function () {
obj.removeClass("blink-class");
t = setTimeout(function () {
blink(IP);
}, 550);
}, 550);
}
blink(IP);
t = setTimeout(function () {
clearTimeout(t);
}, 5000);
}
At first, you shoul always provide the HTML code too :) because now we dont know if the issue is there.
So let's try to solve the problem blindly :)
if i see this correctly, you just go wrong on the element and make it even more complicated than it is since you're using jquery, because if u have an ID on your elem, check this out:
// change this:
var obj = $('input[id="IP"][value=response[i]]') . <---- here // .. is your problem :)
obj.addClass("blink-class");
// with the dot you add this obj, which is it self, on it self :) that cant work :)
// you can try:
var obj = $('input[value="' + response[i] + '"]') // with NO dot and no fixed ID!
obj.addClass("blink-class");
// or try this
var obj = $('#' + response[i]);
obj.addClass("blink-class");
// and put the IP into the ID attraktion of your input element.
Second Problem is you are using an undefined variable "ID":
blink(IP); // in your timeout function
but you didnt declare this var, so if i understand your code right then your response[i] should be the IP?
Your function should look like this:
function BLINK(IP) { // <-- here you need the ip as parameter for your: BLINK(response[i]) from ajax
var t = null;
function blink() {
var obj = $('#' + IP) // and put IP in the id from input
obj.addClass("blink-class")
t = setTimeout(function () {
obj.removeClass("blink-class");
t = setTimeout(function () {
blink(IP);
}, 550);
}, 550);
}
blink(IP);
t = setTimeout(function () {
clearTimeout(t);
}, 5000);
}
try this, if its doesnt work please provide complete html and your css code too, also we could need an eventually error message from console, you can see that by pressing F12 in FireFox or Chrome and then switch to the console tab, press F5 then to reload the page and see errors, post it too please.
Or try out my jsfiddle for you:
https://jsfiddle.net/AIQIA/tjg659sr/17/
You have to remove dots from your IP and put it as id in your elem you wants get to blink, further you need to remove the dots from the response[i] or in your php code before, easy use $ip = preg_replace('/\./','',$ip);
Or use this to use only the complete IP in your input value, then you dont need to remove dots:
https://jsfiddle.net/AIQIA/tjg659sr/21/
greetz Toxi

How to find the column field name when setting template in kendo grid column

I have a list of dynamically generated columns in Kendo grid. I am using here colsList just as an example.
var colsList = ["A", "B", "C"];
for (var j = 0; j < colsList.length; j++){
var columnSchema = {
"field": colsList [j],
template: function (dataItem) {
return getTemplate(dataItem, colsList [j]);
}
};
}
var getTemplate = function (dataItem, field) {
// return tempate format;
};
When getTemplate is called, second parameter i.e. field is always passed here as the last item of colsList.
I need to prepare a column template which will information about the column field associated with it.
How can this be achieved ? Have tried to do this via a number of ways but have not been successful.
I am new to kendo.js and not much familiar with templates.
Is there any other way of preparing template which will help me in achieving what i want to. dataItem and associated col field are the main two requirements while preparing template as on refreshing grid datasource, some conditions need to be checked in template and column data will be filled accordingly.
I have finally found out the way to get the column field by using each function of jQuery instead of using for loop as follows:
var colsList = ["A", "B", "C"];
$.each(colsList, function (index, item) {
var columnSchema = {
"field": item,
template: function (dataItem) {
return getTemplate(dataItem, item);
}
};
});
var getTemplate = function (dataItem, item) {
/* item gives the column field for which template will be set*/
// return tempate format;
};
But still I have no idea why it behaves differently in case of for loop and returns field as undefined in template.

Trouble with method that takes in a function and its arguments

I am having trouble passing a kata. I believe I am on the right track, but do not fully understand how to retrieve the desired results.
The Instructions
Write a method that takes in a function and the arguments to the function and returns another function which when invoked, returns the result of the original function invoked with the supplied arguments.
Example Given
Given a function add
function add (a, b) {
return a + b;
}
One could make it lazy as:
var lazy_value = make_lazy(add, 2, 3);
The expression does not get evaluated at the moment, but only when you invoke lazy_value as:
lazy_value() => 5
Here is my half a day endeavor conclusion
var make_lazy = function () {
var innerFunction = null;
var array = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == 'function') {
innerFunction = arguments[i];
} else {
array.push(arguments[i]);
}
}
innerFunction.apply(innerFunction, array);
innerFunction();
};
I'm using arguments and apply() and think I am close? However I am getting TypeError: lazy_sum is not a function at Object.exports.runInThisContext within test results. Any help, especially understanding what is going on, is appreciated. Thanks
...
return function() {
return innerFunction.apply(this, array);
};
};
Thanks again all. Problem solved.

Calling window.webkitRequestFileSystem twice causes first call not to be bypassed

Have an interesting problem ... I am making a BB10 application (I know) and am using the window.webkitRequestFileSystem call to a) provide a list of local files that this application has downloaded and b) read one of those files and display the contents - nothing crazy.
Individually, I can get both calls a) and b) to work; however, when I place them in the same function hoping to both display a list of files and read one of them, it causes the reading one not to fire at all.
In the example below, regardless of whether the listing call comes before or after the reading call, the reading call will not fire, i.e., I will never see the 'fileentry' alert.
What's going on?
function initFS(){
sharedFolder = blackberry.io.sharedFolder;
var path = sharedFolder+'/downloads';
//alert(path);
document.getElementById('plancontents').value="initialized";
window.requestFileSystem = window.requestFileSystem ||
window.webkitRequestFileSystem;
//window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
//window.requestFileSystem(LocalFileSystem.PERSISTENT, 1024*1024*5,
window.webkitRequestFileSystem(window.PERSISTENT, 1024*1024*5,
function (fs) {
//fs.root.getFile(blackberry.io.sharedFolder
//fs.root.getFile('file:///accounts/1000/shared/downloads/filename.xml', {create: false},
fs.root.getFile(sharedFolder+'/downloads/filename.xml', {create: false},
function (fileEntry) {
alert("fileentry");
fileEntry.file(function (file) {
var reader = new FileReader();
//alert("trying to read file");
reader.onloadend = function (e) {
document.getElementById('plancontents').value = this.result;
};
reader.readAsText(file);
}, errorHandler);
}, errorHandler);
});
window.webkitRequestFileSystem(window.PERSISTENT, 1024*1024*5,
function (fs) {
alert('FS requested and inside');
fs.root.getDirectory(path, {}, function(dirEntry){
alert('inside getDir');
var dirReader = dirEntry.createReader();
alert('created reader');
dirReader.readEntries(function(entries) {
alert('trying to list dir');
for(var i = 0; i < entries.length; i++) {
var entry = entries[i];
if (entry.isDirectory) {
alert('Directory: ' + entry.fullPath);
}
else if (entry.isFile) {
alert('File: ' + entry.fullPath);
}
}
}, errorHandler);
}, errorHandler);
});
}

HTML FileReader

function fileSelected() {
// get selected file element
var files = document.getElementById('files[]').files;
for (var i = 0; i < files.length; i++) //for multiple files
{
(function (file) {
var fileObj = {
Size: bytesToSize(file.size),
Type: file.type,
Name: file.name,
Data: null
};
var reader = new window.FileReader();
reader.onload = function (e) {
fileObj.Data = e.target.result;
};
// read selected file as DataURL
reader.readAsDataURL(file);
//Create Item
CreateFileUploadItem(fileObj);
})(files[i]);
}
}
function CreateFileUploadItem (item) {
console.log(item);
$('<li>', {
"class": item.Type,
"data-file": item.Data,
"html": item.Name + ' ' + item.Size
}).appendTo($('#filesForUpload'));
}
So when console.log(item) gets run in the CreateFileUploadItem function it shows the item.Data. YET it won't add it to the data-file of the LI. Why is that?
The call to readAsDataURL is asynchronous. Thus, the function call is likely returning prior to the onload function being called. So, the value of fileObj.Data is still null when you are attempting to use it in CreateFileUploadItem.
To fix it, you should move the call to CreateFileUploadItem into your onload function. As for the console logging the proper value, you can't rely on that being synchronous either. I think using a breakpoint during debugging at that line instead will likely show the true null value.