HERE Maps API outputting function results too many times - javascript

having some issues with the HERE maps api outputting the data i'm requesting too many times. I need it to output only once every 10 seconds, however it is doing it far more times than that.
The code I have here is as follows:
function getMapCorners() {
var mapView = map.getViewModel();
var mapCorners = mapView.b.bounds.cb.X;
var corners = [mapCorners[9], mapCorners[10], mapCorners[3], mapCorners[4]];
console.log(corners);
}
map.addEventListener("mapviewchange", function () {
setTimeout(getMapCorners, 10000);
});
I need to grab these specific co-ordinates as this is feeding into another API that is creating markers on the map in specific areas. When zooming in, it does wait 10 seconds to run the function, but then it throws out the co-ordinate changes for every change in the map's view model during that 10 seconds.
The other API has a request limit of 100 every 15 minutes. So I need this to change so that it will run the function every 10 seconds, and it will only output the information for the final viewModel when the function runs.

If you want to run getMapCorners once every 10 seconds, then do this:
function doEveryTenSeconds(){
getMapCorners();
setTimeout(doEveryTenSeconds, 10000);
}
doEveryTenSeconds();

First, please don't use internal name of objects like .b.bounds.cb.X - b, cb, X are defined by compilation of js library and will be changed for new version of JS API. Please use for it a name of methods and objects described in documentation: https://developer.here.com/documentation/maps/3.1.29.0/api_reference/H.map.ViewModel.html#getLookAtData like:
var mapCorners = map.getViewModel().getLookAtData().bounds.getBoundingBox();
Second, the map event 'mapviewchange' runs multiply times during interacting with map therefore you get multiply times run of function getMapCorners. Suggestion to achieve what you want:
function getMapCorners() {
console.log("getMapCorners!!!", map.getViewModel().getLookAtData().bounds.getBoundingBox());
map.addEventListener("mapviewchange", onMapViewChange);
}
function onMapViewChange() {
map.removeEventListener("mapviewchange", onMapViewChange);
setTimeout(getMapCorners, 10000);
}
See please full code there on https://jsfiddle.net/1m8fvLjy/1/

Related

How to use the Firebase real time database for web application

In my website I'm Showing my database after user has given the database name, Is there any way I can constantly update the web shown databasebase without refreshing the page . I've tried using setInterval but it's not working for some reason .
function c(){
setInterval(beta, 1000);
}
function beta(){
var d = document.getElementById("opopo").value;
var firebaseRefff= firebase.database().ref('LOCATION/'+d);
firebaseRefff.on('child_added', snap=> {
var slot=snap.getKey();
var alloted=snap.child("ALLOTED").val();
var date=snap.child("DATE").val();
var limit=snap.child("LIMIT").val();
var time=snap.child("TIME").val();
$("table tbody").append(""+slot+""+alloted+""+date+""+limit+""+time+"Null");
});
}
You do not need, and should not use, setInterval to trigger the queries. What you have in your beta() function looks pretty good.
firebaseRefff.on('child_added', snap => {}) means "whenever a child is added under this location, trigger the callback function (empty in my example) with the parameter 'snap'". It will also be called once, initially, for each child that is already at that database reference location.
You need to make sure you've called beta() once to setup this trigger.
If you're still having problems, you might want to insert logging to make sure beta() is being called, what the full reference path is, if the callback is ever triggered, and if your jquery string is correct.

Calling a function within a function (invoke) in Google Apps Script

I'm automating some processes within Google Apps Script.
I have created a few functions that are currently independent from each other.
However now, I want to regroup them so's they are triggered within the "master" function.
function master(){
// code that imports data from a form and organises it
runMeAfterMaster();
}
function runMeAfterMaster(){
// code that should run after master
}
Both are in the same script file, both work independently but I can't seem to just be able to "invoke" or call my other function within the master one.
Please Help!
I ran into a similar issue when my function names started with lower case letters.
the error I got was:
TypeError: randomNumber is not a function
my code:
/*
goal of the script is to highlight project rotators
*/
function HightlightProject()
{
// get random number
var randomNumber = randomNumber();
}
function randomNumber()
{
// get cell from certain spread sheet
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange("fundation rotator!B7");
// random number
var number = Math.floor(Math.random() * 100);
// set random number
cell.setValue(number);
}
if I capitalize randomNumber to RandomNumber, the function names turn pink, and it works.
/*
goal of the script is to highlight project rotators
*/
function HightlightProject()
{
// get random number
var randomNumber = RandomNumber();
}
function RandomNumber()
{
// get cell from certain spread sheet
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange("fundation rotator!B7");
// random number
var number = Math.floor(Math.random() * 100);
// set random number
cell.setValue(number);
}
What you wrote is ok runMeAfterMaster look to be invoked, I suppose your problem is elsewhere have you decorated your functions with Logger.log?
eg:
function master(){
// code that imports data from a form and organises it
Logger.log("I'm in master now");
runMeAfterMaster();
Logger.log("still in master but after calling runMeAfterMaster");
}
function runMeAfterMaster(){
Logger.log("I'm in runMeAfterMaster now");
// code that should run after master
Logger.log("getting out of runMeAfterMaster");
}
if you can't use Logger.log because the function is triggered automatically (and you can't have a look at the logs) yoo can replace it by your own loggin function that write everything in a spreadsheet:
function logit(message) {
SpreadsheetApp.openById("SPREADSHEET_ID").getActiveSheet().appendRow([new Date(),message]);
}
then it will become:
function master(){
// code that imports data from a form and organises it
logit("I'm in master now");
runMeAfterMaster();
logit("still in master but after calling runMeAfterMaster");
}
function runMeAfterMaster(){
logit("I'm in runMeAfterMaster now");
// code that should run after master
logit("getting out of runMeAfterMaster");
}
Harold's tips put me on the right path. The path to testing things out.
I placed my code in a code editor (in my case Brackets) and ran it through JSLint. To find any errors.
No errors there, even though the curly bracket at the end of the function name was red. It turns out over 100 lines of code in google apps and the curly bracket turns red. Under 100 lines you're good to go!
Unfortunately i couldn't debug.
So i decided to make it simpler and call both functions within a new one. That didn't work...and I still ignore why.
function MasterOfAll(){
Master();
runAfterMaster();
}
Second thing I did was to simply regroup both code of each function in a new one and call it. That worked.
function MasterOfAll(){
//Code from the Master function.
//Code from the runAfterMasterFunction.
}
Needless to say, I'm not a big fan of this solution (unclear, messy) but it works!

continously run a javascript function

I am designing a web-page to allow users to edit geographical features on the fly. I use a simple function to calculate road lengths that they have been created. The issue is that I have to reload the page to update the statistics. The function I use to create the statistics is below, how can I make this run to update the stat's without reloading the page:
function postExecute() {
var roadLengthCalc = Math.round(RoadLength['RoadLength']);
document.getElementById("Road_Length").innerHTML = roadLengthCalc;
}
You can use the setInterval function:
For example:
setInterval(function(){ postExecute(); },3000);
The 3000 is the frequency in milliseconds.
Documentation:
You can find the full documentation for this function at the link below:
http://www.w3schools.com/jsref/met_win_setinterval.asp
JSFiddle working example:
http://jsfiddle.net/d5DkA/

pdf 3d with JavaScript : simple example

I need to do a simple pdf with some 3D objects for an oral presentation. I made several views, each one with a camera viewpoint, an object and a display mode.
To avoid the need to manually switch between the different viewpoints with the context menu, I would like the viewpoints to switch automatically with a Timer (each viewpoint staying for a few seconds). And I would like to not have to touch the mouse at all (nor the keyboard), so I would like to have the playback started as soon as the page appears.
I found the javascript command runtime.setView(N,x) to switch to the x'th view among N, but I don't know where to put it (I don't want to define a function which will be called when I press a button, since I want everything to be automated). Also, I don't know how to pause for a few seconds.
Any help ? Thanks !
I believe you're looking for setInterval(fn, time) which will call a function periodically at a time interval that you set. I'm not familiar with the setView() method you mentioned, but here's some pseudo code that you would put in tags at the end of the document body.
function startSwitcher()
var viewNum = 0;
var maxViews = 5; // you set this to how many views there are
setInterval(function() {
++viewNum;
if (viewNum >= maxViews) {
viewNum = 0;
}
runtime.setView(N, viewNum); // you will have to figure out this line
}, 2000);
}
startSwitcher();
The 2000 is 2000 milliseconds and is the time interval between executing the function. You can put any number of milliseconds there.
The runtime.setView(N, viewNum) line is something you will have to figure out as I am not familiar with whatever library you're trying to use there. The operative part of this code is the viewNum variable which configures which view in rotation should be next.
I think the runtime.SetView(..) Methods works with the name of the view as a string instead of the viewnumber. I have this function in a Dokument-level script and it works for me:
// view is the name of the view for example "TopView"
function setView(view){
console.println("Navigating to view: "+view);
var pageIndex = this.pageNum;
var annotIndex = 0;
var c3d = this.getAnnots3D( pageIndex )[ annotIndex ].context3D;
c3d.runtime.setView(view, true);
}
Combine this with the setInterval(..) from jfriend00´s answer und you should get what you need.
Best regards

How can I dynamically change a JS called from a client?

I have a html page with 2 buttons on it and a big javascript function that is executed every 10 ms. Each click on each button executes a javascript function that changes some variables used by the big javascript function and change its behaviour. In time the code got very complex and time consuming and I would like to move the entire calculation of variables over to the server side. Or, better yet I would like to generate the entire function on the server.
How can I dynamically change the big javascript function when any of the 2 buttons is being pressed?
What you could do is once the button is pressed you make an Ajax request to the server side. There you generate the javascript code based on the button pressed and send it back. You can then eval the response and execute the appropriate function.
Grabbing a large script and executing it every 10 ms is bound to be slow.
Modifying a large script isn't that bad and then you only care about the execution plan of the script. (It doesn't matter how fast the server can generate the script the client will execute if you're sending it every 10 ms and it's super complicated. Client execution is going to cause the client to creep if not properly made.)
However have you considered just "fixing" your script so that it's a bit more compartmentalized. Surely the entirety of the function does not change every 10 ms? (On your site does it change from being a game of Pong to a looping weather radar graphic every 10 ms?)
compartimentalized javascript example (using jquery): Assumes you have two tags one with the class buttonA and one with the class buttonB. Clicking buttonA or buttonB changes internal values and then the script is executed.
var complicatedFunction = function(){
//private variables and methods
var a = 2, b = 3;
//public variables and methods
return {
exec: function(el){
//which button was pushed?
if($(el).is('.buttonA')){
a = 4;
b = 8;
} else {
a = 10;
b = 2;
}
alert('The result of a*b is ' +(a*b));
}
}
}();
$('.buttonA, .buttonB').click(function(){complicatedFunction.exec(this)});
Wish I could help more, if you post a code example maybe we can!

Categories