Not able to retrieve callback parameters - callback

var arr = [1,2,3,4,5,6];
Array.prototype.filter2 = function(){
var fir = arguments[0];
var sec = arguments[1];
alert(fir);
alert(sec);
var arr2 = new Array();
for(var item=0; item<this.length;item++){
alert(this[item]); // 1 section
if(sec.call(this[item],fir)){
arr2.push(this[item]);
}
}
return arr2;
}
function xyz (ele ,x){
alert(x);
alert(ele);
return ele > x;
}
arr.filter2(2,xyz);
Till 1'st its running fine but while passing the argument to the callback... x is being alerted as "undefined" whereas ele = fir

Your answer is simple and straight:
either call your function normally,.. i.e
Array.prototype.filter2 = function(){
var fir = arguments[0];
var sec = arguments[1];
alert(fir);
alert(sec);
var arr2 = new Array();
for(var item=0; item<this.length;item++){
alert(this[item]); // 1 section
if(sec(this[item],fir)){
arr2.push(this[item]);
}
}
return arr2;
}
Or if you still want to stick with javascript .call(), then simply use it like this:
Array.prototype.filter2 = function(){
var fir = arguments[0];
var sec = arguments[1];
alert(fir);
alert(sec);
var arr2 = new Array();
for(var item=0; item<this.length;item++){
alert(this[item]); // 1 section
if(sec.call({},this[item],fir)){
arr2.push(this[item]);
}
}
return arr2;
}
call() and apply() are predefined JavaScript function methods. Both methods can be used to invoke a function, and both methods must have the owner object as first parameter.
You can read more about this by clicking here and find it under heading Invoking a Function with a Function Method

Related

Insert multiple elements in .bindPopup in Leaflet

I have a function which extracts multiple elements from JSON datatype. I try to bind now multiple elements from my function in my popup divided by paragraphs, but I'm not able to set multiple variables in the popup. How can integrate multiple elements? I tried paste() or & but it won't work.
function getUsers() {
$.getJSON("getData.php", function (data) {
for (var i = 0; i < data.length; i++) {
var location = new L.LatLng(data[i].lat, data[i].lng);
var species = data[i].species;
var diameter = data[i].average_diameter;
var quality = data[i].quality;
var damage = data[i].damage;
var notes = data[i].additional_information;
var marker = L.marker([data[i].lat, data[i].lng], {icon: greenIcon}).addTo(map);
marker.bindPopup(diameter);
}
})
}
You can do this:
marker.bindPopup(diameter + "|" + quality);
// or
marker.bindPopup(`${diameter} | ${quality}`);

why writing function of setTimeout separately would not work

I am trying to build 'Simon Game' and used setTimeout method to lighten the buttons. when I write the the functions directly inside the setTimeout method it works.
$(function(){
var randPattern=[];
var buttonId=['red','yellow','green','blue'];
var origColor=['#B40404','#D7DF01','#0B610B','#0B0B61'];
var patternColor=['#F78181','#F4FA58','#A9BCF5','#81F781'];
var level= 5;
var count= 0;
var time= 1000;
$('#start').click(function gameStart() {
patternGenerate();
for(i=0; i<randPattern.length; i++){
display(i);
}
});
function patternGenerate() {
for(var h=0; h<level; h++){
randPattern.push( Math.floor( Math.random()*4) );
}
}
function display(i){
setTimeout(function(){document.getElementById(buttonId[randPattern[i]]).style.backgroundColor = patternColor[randPattern[i]];document.getElementById(randPattern[i]).play();}, 1500*i);
setTimeout(function(){document.getElementById(buttonId[randPattern[i]]).style.backgroundColor = origColor[randPattern[i]]}, 750+1500*i );
}
but when I write the functions that is going inside setTimeout method separately, such function a(i), and function b(i). It doesn't work. And the console says ''cannot read the property 'style' of null''. I think there is no difference between these two. I can't understand why the second way doesn't work while the first one does.
$(function(){
var randPattern=[];
var buttonId=['red','yellow','green','blue'];
var origColor=['#B40404','#D7DF01','#0B610B','#0B0B61'];
var patternColor=['#F78181','#F4FA58','#A9BCF5','#81F781'];
var level= 5;
var count= 0;
var time= 1000;
$('#start').click(function gameStart() {
patternGenerate();
for(i=0; i<randPattern.length; i++){
display(i);
}
});
function patternGenerate() {
for(var h=0; h<level; h++){
randPattern.push( Math.floor( Math.random()*4) );
}
}
function a (i){
document.getElementById(buttonId[randPattern[i]]).style.backgroundColor = patternColor[randPattern[i]];
document.getElementById(randPattern[i]).play();
}
function b (i){
document.getElementById(buttonId[randPattern[i]]).style.backgroundColor = origColor[randPattern[i]];
}
function display(i){
setTimeout(a,1500*i);
setTimeout(b,750+1500*i);
}
});
a and b expect to be passed an argument which will be assigned to i.
While you have a variable called i in the display function: You aren't passing it to a or b.
function display(i){
setTimeout( a, 1500*i, i );
setTimeout( b, 750+1500*i, i );
}
You are using an i inside function display(i){ but this variable is not passed to a or b. If you want to pass a variable, you still need to create a new closure and call your function from it:
function display(i){
setTimeout(function() { a(i) }, 1500*i);
setTimeout(function() { b(i) }, 750+1500*i);
}

Append divs changing his content dynamic

I have a dif called cdefualt that has some inputs from a form inside of it and I want to do something like this to clone it and change that input names:
var i = 2;
function add() {
var item = $('#cdefault').clone();
item.attr({'style': ''});
$xpto = 'gtitle'+i;
$xpto2 = 'gmessage'+i;
item.id = $xpto;
$('#'+$xpto+' input[id="gtitle1"]').attr('name', $xpto);
$('#'+$xpto+' textarea[id="gmessage1"]').attr('name',$xpto2);
$(item).appendTo('#ccontainer');
i++;
}
But this doesnt work. I've tried this already as well but it only works twice (for the original and first clone):
var i = 2;
function add() {
var item = $('#cdefault').clone();
item.attr({'style': ''});
$xpto = 'gtitle'+i;
$xpto2 = 'gmessage'+i;
$('#cdefault input[id="gtitle1"]').attr('id', $xpto);
$('#cdefault textarea[id="gmessage1"]').attr('id',$xpto2);
$('#cdefault input[name="gtitle1"]').attr('name', $xpto);
$('#cdefault textarea[name="gmessage1"]').attr('name', $xpto2);
$(item).appendTo('#ccontainer');
i++;
}
Even tryed this way:
function add() {
$xpto = 'gtitle'+i;
$xpto2 = 'gmessage'+i;
var div = document.getElementById('cdefault');
clone = div.cloneNode(true); // true means clone all childNodes and all event handlers
clone.id = $xpto;
clone.style.display = '';
$("#"+$xpto+" input[id='gtitle1']").attr('name', $xpto);
$("#"+$xpto+" textarea[id='gmessage1']").attr('name',$xpto2);
document.getElementById('ccontainer').appendChild(clone);
i++;
}
http://jsfiddle.net/Theopt/xNfSd/
fixed. changed cdefault id to id0 and this java script:
var i = 2;
var c = 0;
function add() {
$xpto = 'gtitle'+i;
$xpto2 = 'gmessage'+i;
var klon = $( '#id'+ c );
klon.clone().attr('id', 'id'+(++c) ).insertAfter( '#inserthere' );
document.getElementById('id'+(c)).style.display = '' ;
$("#id"+(c)+" input[id='gtitle1']").attr('name', $xpto);
$("#id"+(c)+" textarea[id='gmessage1']").attr('name',$xpto2);
i++;
}

Google apps script listbox event processing, undefined value

The idea of this apps script is to populate listbox LbxJobs based on the selected item in listbox LbxJobTypes.
I am getting an undefined value for e.parameter.LbxJobType which seems to be preventing conditional population of LbxJobs.
I successfully tested the handler(JobTypeValueHandler) by hard coding(I set JobType=T300_JOB_TYPE) JobTypeValueHandler function and the LbxJobs listbox populated as expected.
I do get "LbxJobType" when checking e.parameter.source. The listboxes were created in GUI builder.
var T200_JOB_TYPE = 1;
var T300_JOB_TYPE = 2;
function doGet() {
var app = UiApp.createApplication();
app.add(app.loadComponent("BasicCalculator"));
var Dischandler = app.createServerHandler('DiscClickHandler');
app.getElementById('chbxDiscl').addValueChangeHandler(Dischandler);
return app;
}
function DiscClickHandler(e) {
var app = UiApp.getActiveApplication();
var Discpanel = app.getElementById('FLpnlDisc');
BCalcSetup(app);
Discpanel.setVisible(false);
return app;
}
function BasicClickHandler(e) {
var app = UiApp.getActiveApplication();
return app;
}
function BCalcSetup(app){
var BCalcpanel = app.getElementById('APnlBCalc');
var lbxJobType = app.getElementById('LbxJobType');
var JobTpyehandler = app.createServerChangeHandler('JobTypeValueHandler');
var lbxJobs = app.getElementById('LbxJobs');
JobTpyehandler.addCallbackElement(lbxJobType);
lbxJobType.addChangeHandler(JobTpyehandler);
lbxJobType.addItem('Title 200');
lbxJobType.addItem('Title 300');
loadClassifications(lbxJobs,T200_JOB_TYPE);
BCalcpanel.setVisible(true);
}
function JobTypeValueHandler(e) {
var app = UiApp.getActiveApplication();
var JobType=T200_JOB_TYPE;
var lboxJobs=app.getElementById('LbxJobs');
if (e.parameter.LbxJobType=='Title 300'){JobType=T300_JOB_TYPE;}
loadClassifications(lboxJobs,JobType);
app.close();
return app;
}
function loadClassifications(lbox,JobType){
var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID);
lbox.clear();
if (JobType==T300_JOB_TYPE){
var T3data =spreadsheet.getRangeByName('Title300Jobs').getValues();
for (var row1 = 1; row1 < T3data.length; row1++) {
lbox.addItem(T3data[row1]);
}
}else{
var T2data =spreadsheet.getRangeByName('Title200Jobs').getValues();
for (var row2 = 1; row2 < T2data.length; row2++) {
lbox.addItem(T2data[row2]);
}
}
}
I feel kind of silly but I figured this out. The problem I had was I did not fill in the Name property for these listboxes when I created the interface in GUI builder. I went back into GUI builder and filled in the name property for each box and and now it works like a champ. Live and learn

'Not A Member of Object' Error

I think I'm beginning to like this coding stuff. Anyway in my current Shooting Gallery project I have a JavaScript question. I'm building in Unity3d and I get a "transform" is not a member of "Object" error on the code inserted below.
var newball;
static var tempBasketBall :Rigidbody;
private var canFire = true;
var pos :Transform[];
var ball1 :Rigidbody;
var canControl1 = true;
var destroyTime :int = 6;
var player1 :GameObject;
var b1Parent :Transform;
var yVel :float;
var zVel :float;
function Start()
{
ball1 = Instantiate (tempBasketBall, pos[0].position, pos[0].rotation);
ball1.transform.parent = b1Parent;
}
function Update() {
if(Input.GetButton("Fire1"))
animation.PlayQueued("fire", QueueMode.PlayNow);
}
function TapFunction() {
animation.PlayQueued("fire", QueueMode.PlayNow);
player1.animation.PlayQueued("fire");
ball1.transform.parent = null;
ball1.useGravity = true;
ball1.velocity = transform.TransformDirection(0, yVel, zVel);
MakeBall1(pos[0]);
canControl1 = false;
player1.animation.PlayQueued("idle");
}
function MakeBall1(pos)
{
yield new WaitForSeconds(1);
ball1 = Instantiate(tempBasketBall, pos.transform.position, pos.transform.rotation);
ball1.transform.parent = b1Parent;
canControl1 = true;
}
The error is in the MakeBall function at the end. To my untrained mind, it seems I established the
transform in the start function. As usual any assistance and shared knowledge will be tremendously appreciated.
Transform(you are passing as argument an onject of this tipe) does not have a "transform" member,you should use pos.position