requestAnimationFrame + apply - requestanimationframe

Var T=0
...
var TestClass = new Class()
TestClass.extend({
init: function () {
this.TT = T;
T++
}
});
TestClass.include({
animate: function () {
A = this
window.requestAnimationFrame(function () {
A.animate();
console.log(A.TT)
});
}
});
...
var can1 = new TestClass();
var can2 = new TestClass();
var can3 = new TestClass();
...
var can1.animate()
var can2.animate()
var can3.animate()
So it works only for can3.
console: >>2
if i will do:
TestClass.include({
animate: function ()
{
console.log(this.TT)
}
});
var can1 = new TestClass();
var can2 = new TestClass();
var can3 = new TestClass();
function G() {
window.requestAnimationFrame(function () {
can1.animate()
can2.animate()
can3.animate()
G()
});
}
G()
it will works correctly, but how i can do requestAnimationFrame inside each of TestClass?

I have same issue. So make it.
Try it?
https://github.com/mixed/requestAnimationFrameInterval
TestClass.include({
animate: function () {
A = this
window.requestAnimationFrameInterval(function () {
A.animate();
console.log(A.TT);
});
}
});
var can1 = new TestClass();
var can2 = new TestClass();
var can3 = new TestClass();
var can1.animate()
var can2.animate()
var can3.animate()
`

Related

meteor template helpers function not working properly

I have 2 functions in Template.meetings.helpers, but only one function is working.
Template.meetings.helpers({
group: function() {
console.log('meeting id from helper group = ' + this.meetingId);
console.log('group id from helper group = ' + this.groupId);
return Groups.findOne({_id: this.groupId});
},
meeting: function() {
console.log('meeting id from helper meeting = ' + this.meetingId);
console.log('group id from helper meeting = ' + this.groupId);
return Meetings.findOne({_id: this.meetingId});
},
});
My console shows the following:
router.js:73 meetingId from router = ZKkLLvdCpRmrF7uXD
router.js:74 groupId from router = xFSzAHBEps2dSKcWM
meetings.js:26 meeting id from helper meeting = ZKkLLvdCpRmrF7uXD
meetings.js:27 group id from helper meeting = xFSzAHBEps2dSKcWM
router.js:
Router.route('/meetings/:_id/:groupId?', {
waitOn: function() {
return Meteor.subscribe('meeting');
return Meteor.subscribe('Group');
},
action: function () {
console.log('meetingId from router = ' + this.params._id);
console.log('groupId from router = ' + this.params.groupId);
this.render('meetings', {
data: {
meetingId: this.params._id,
groupId: this.params.groupId
}
});
},
onBeforeAction: function(){
var currentGroup = Members.find({
groupId: this.params.groupId,
memberId: Meteor.userId()
}).count();
Session.set('groupId', this.params.groupId);
var currentUser = Meteor.userId();
if(currentUser && currentGroup > 0){
this.next();
} else {
this.render("landingPage");
}
}
});
I can't figure out why the group: function() is not outputting to the console.
Any suggestions?

Unable to show csv data in d3.geo map

i am trying to show csv data in my map .The map i created from json data . But now it doesn't show any csv data but showing only json data.
tootip.html(" District :"+"\n" +d.id +d.Family_Feud_m)
here d.id 's "id" is from json file and d.Family_Feud_m's "Family_Feud_m" from csv file .
Here is my full js code:
var Districts=[];
var bardata = [];
var width = 500,
height = 700;
var scal = 5000;
var tootip = d3.select('body')
.append('div')
.style('position','absolute')
.style('padding','0 10px')
.style('background','white')
.style('opacity',0)
var projection = d3.geo.mercator()
.scale(scal)
.translate([-76.5/50.0 * scal, scal/2-scal/55]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select(".viewer")
.append("svg")
.attr("width", width)
.attr("height", height);
function redrawMap()
{
svg.selectAll(".subunit")
.style('fill',function(d){
amount = Districts[d.id];
if(amount == 0 || amount== undefined)
return 'rgba(0,0,0,0.5)';
return 'rgba(43,120,200,'+amount*1.0/10.0+')';
});
}
d3.csv('./data/resultsuicide.csv',function(data){
console.log(data)
for(key in data) {
bardata.push(data[key].value)
}
d3.json("./data/bd.json", function(error, bd) {
svg.selectAll(".subunit")
.data(topojson.feature(bd, bd.objects.subunits).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + d.id; })
.attr("d", path)
.on('mouseover',function(d){
tootip.transition()
.style('opacity',.9)
tootip.html(" District :"+"\n" +d.id +d.Family_Feud_m)
.style('left',(d3.event.pageX-35)+'px')
.style('top',(d3.event.pageY-30)+'px')
tempcolor = this.style.fill;
d3.select(this)
.style('opacity',.5)
.style('fill','green')
})
.on('mouseout',function(d){
d3.select(this)
.style('opacity',1)
.style('fill',tempcolor)
})
redrawMap();
});
});
//thanks in advance

Geocoder does not return the location properly

I have two arrays containing latitude and longtitude of 7 locations,and I use the following
code for getting the location of these 7 points:
var geocoder;
initialize();
codeLatLng();
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeLatLng() {
var arrlat=[45.95268273, 47.00196833,45.99168238, 46.2186456, 45.91612197, 45.91606814, 45.91606814];
var arrlon=[-66.68426012, -65.56480221, -63.98068545, -64.44419501, -66.74801471, -66.74810832, -66.74810832];
var input = document.getElementById("latlng").getAttribute('value');
console.log(input);
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
for(var i=0;i<arrlat.length;i++)
{
var latlng = new google.maps.LatLng(arrlat[i], arrlon[i]);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
//document.getElementById("test").innerHTML = '' + (results[4].formatted_address); + ''
alert(results[4].formatted_address);
});
}
}
whenever I feed the arrays with 5 of less elements the code works properly but for more than 5 I get 5 alert showing the location of the first 5 elements but for the last 2 I get the following error:
Uncaught TypeError: Cannot read property '4' of null
And here is the Jfiddle link:
link
Can anyone help?
Code more defensively. Check the status of the response before using it. If it isn't "OK", there won't be any results to process.
function codeLatLng() {
var arrlat=[45.95268273, 47.00196833,45.99168238, 46.2186456, 45.91612197, 45.91606814, 45.91606814];
var arrlon=[-66.68426012, -65.56480221, -63.98068545, -64.44419501, -66.74801471, -66.74810832, -66.74810832];
var input = document.getElementById("latlng").getAttribute('value');
console.log(input);
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
for(var i=0;i<arrlat.length;i++)
{
var latlng = new google.maps.LatLng(arrlat[i], arrlon[i]);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK ) {
//document.getElementById("test").innerHTML = '' + (results[4].formatted_address); + ''
if (results.length >= 5)
alert(results[4].formatted_address);
else alert("less than 5 results");
} else alert("reverse geocode failed, status="+status);
});
}
}
See the documentation on Status Codes for what the failure codes mean.

Google Maps API: Uncaught TypeError: Cannot read property '__e3_' of undefined

I'm currently use a new system for my weather website and I'm getting the following error:
Uncaught TypeError: Cannot read property '__e3_' of undefined
Xe%7Bmain,places,weather%7D.js:18
Ve%7Bmain,places,weather%7D.js:21
P.addListener%7Bmain,places,weather%7D.js:18
T.(anonymous function).bindTo%7Bmain,places,weather%7D.js:27
initializejavascript.js:109
(anonymous function)javascript.js:4
f.Callbacks.ojquery.min.js:2
f.Callbacks.p.fireWithjquery.min.js:2
e.extend.readyjquery.min.js:2
c.addEventListener.B
Here's the content in my javascript.js file which I include in my header.php (<script src="javascript.js" type="text/javascript"></script>):
$(document).ready(function() {
initialize();
});
var elevator;
var geocoder;
var map;
function initialize() {
// KONFIGURATION: Geocoding
geocoder = new google.maps.Geocoder();
// KONFIGURATION: Filnamn
var weatherdata = 'fetch-information/fetch-weatherdata.php';
// KONFIGURATION: Datum
var date = new Date();
var datetime = date.getFullYear() + '-' +
(date.getMonth() < '10' ? '0' + date.getMonth() : date.getMonth()) + '-' +
(date.getDay() < '10' ? '0' + date.getDay() : date.getDay()) + ', ' +
(date.getHours() < '10' ? '0' + date.getHours() : date.getHours()) + ':' +
(date.getMinutes() < '10' ? '0' + date.getMinutes() : date.getMinutes()) + ':' +
(date.getSeconds() < '10' ? '0' + date.getSeconds() : date.getSeconds());
// KONFIGURATION: Hämta adress
var address = 'Karlstad, Sverige';
// KONFIGURATION: Inställningar för kartan
var myOptions = {
streetViewControl: false,
overviewMapControl: true,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// MOLNAKTIVITET
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
/**********************************************
** THE CODE BELOW IS CAUSING THE ERROR **
**********************************************/
// HÖJD ÖVER / UNDER HAVSYTAN
elevator = new google.maps.ElevationService();
google.maps.event.addListener(map, 'click', getElevation);
/* ** ** ** ** ** */
// GOOGLE PLACES API
var input = document.getElementById('googlemaps-search');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
alert(place.geometry.location);
});
/**********************************************
** THE CODE ABOVE IS CAUSING THE ERROR **
**********************************************/
/* ** ** ** ** ** */
// GEOCODING
geocoder.geocode({'address' : address}, function(results, status) {
// KONTROLL
if(status == google.maps.GeocoderStatus.OK) {
// KONFIGURATION: Ta bort ( och ) före och efter koordinaterna
var old_latlong = '' + results[0].geometry.location + '';
var new_latlong = old_latlong.substring(1, old_latlong.length - 1).replace(' ', '');
// CENTERRING
map.setCenter(results[0].geometry.location);
// MARKERING: Adress
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
// AJAX: Hämta den angivna filen
$.ajax({
url: weatherdata + '?coordinates=' + new_latlong,
success: function() {
console.log(datetime + ': Hämtningen av väderprognosen och den övriga informationen lyckades');
$('.content-search').show();
$('#load-weather').load(weatherdata + '?coordinates=' + new_latlong);
},
error: function() {
console.error(datetime + ': Hämtningen av väderprognosen och den övriga informationen misslyckades');
$('#load-weather').hide();
$('.content-search').hide();
$('#message-error').show();
}
});
// KONTROLL
} else {
// FELMEDDELANDE
alert('Kunde inte hämta data på grund av följande fel: ' + status);
}
});
/* ** ** ** ** ** */
// KARTA
var map = new google.maps.Map(document.getElementById('weather-map'), myOptions);
}
/** ** ** ** ** ** ** ** ** ** ** ** **/
function getElevation(event) {
var locations = [];
var clickedLocation = event.latLng;
locations.push(clickedLocation);
var positionalRequest = {
'locations': locations
}
elevator.getElevationForLocations(positionalRequest, function(results, status) {
if(status == google.maps.ElevationStatus.OK) {
var s = results[0].elevation;
if(results[0]) {
if(('' + parseInt(s)).charAt(0) == '-') {
$('#elevation').html('Cirka ' + number_format(('' + parseInt(s)).substring(1)) + ' meter under havsytan');
} else {
$('#elevation').html('Cirka ' + number_format(parseInt(s)) + ' meter över havsytan');
}
} else {
alert('Inget resultat hittades');
}
} else {
alert('Det gick inte att hitta höjdskillnaden på grund av följande: ' + status);
}
});
}
The problem is that the system is quite the same as in my previous system and I have the same "code structure" on both of them and still I'm getting this error message on the new system. I don't know why I'm getting it so I'm asking you know: how can I fix my problem?
Thanks in advance.
When I moved the map definition to a point before the marked buggy code, it worked. I added the var map = new google.map.Map to right after the map options.
var myOptions = {
streetViewControl: false,
overviewMapControl: true,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('weather-map'), myOptions);
// MOLNAKTIVITET
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
/**********************************************
** THE CODE BELOW IS CAUSING THE ERROR **
**********************************************/
// HÖJD ÖVER / UNDER HAVSYTAN
elevator = new google.maps.ElevationService();
google.maps.event.addListener(map, 'click', getElevation);

How to count duplicate items in XMLList and assign them into an ArrayCollection?

I've following XMLList ,
<party/>
<party/>
<party/>
<party>A</party>
<party>B</party>
<party>C</party>
<party>A</party>
<party>B</party>
<party>C</party>
<party>D</party>
<party>E</party>
<party>D</party>
<party>A</party>
<party/>
<party>C</party>
I would like eliminate blank node and to make an ArrayCollection like ( with count of individual party),
tArr = new ArrayCollection([ {Party:"A", Count:3},
{Party:"B", Count:2},
{Party:"C", Count:3},
{Party:"D", Count:2},
{Party:"E", Count:1},
]);
Thanks in advance.
This is untested and may not be the most efficient, but should work:
var partyDict:Dictionary = new Dictionary();
var parties:ArrayCollection = new ArrayCollection();
var xml:XML = <root><party/><party/><party/><party>A</party><party>B</party><party>C</party><party>A</party><party>B</party><party>C</party><party>D</party><party>E</party><party>D</party><party>A</party><party/><party>C</party></root>;
for each (var p:XML in xml.party) {
var val:String = p.toString();
if ((val != null) && StringUtil.trim(val).length > 0) {
if (partyDict[val] != null) {
partyDict[val] = (partyDict[val] as int) + 1; // may simply be able to do partyDict[val]++;
} else {
partyDict[val] = 1;
}
}
}
for (var key:Object in partyDict) {
var o:Object = new Object();
o.Party = key;
o.Count = partyDict[key];
parties.addItem(o);
}
If you have the list of possible parties it's just:
var partiesObjs:ArrayCollection = new ArrayCollection();
var xml:XML = <root><party/><party/><party/><party>A</party><party>B</party><party>C</party><party>A</party><party>B</party><party>C</party><party>D</party><party>E</party><party>D</party><party>A</party><party/><party>C</party></root>;
var parties:Array = ["A","B","C","D"]
for each(var p:String in parties){
var count:int = xml..party.(toString() == p).length()
partiesObjs.addItem( {Party:p, Count:count} )
}

Resources