0

I am using Leaflet to add 3 markers on a floor plan. It works fine. then I tried to let Math.random to produce the coordinates in pixels every 3 secs, remove the old marker and add a new marker. But I found the setTimeout() doesn't work. I tried many solutions, all failed. Please could you guide?

one more question: the control.scale doesn't work either. The scale is still at the bottomleft and ft bar is still there.

I am using Leaflet to add 3 markers on a floor plan. It works fine. then I tried to let Math.random to produce the coordinates in pixels every 3 secs, remove the old marker and add a new marker. But I found the setTimeout() doesn't work. I tried many solutions, all failed. Please could you guide?

one more question: the control.scale doesn't work either. The scale is still at the bottomleft and ft bar is still there.

  <script>

    var map = L.map('map', {
    crs: L.CRS.Simple,
    Zoom: 0,
    maxZoom:16,
    minZoom: -5
  });



    var bounds = [[0,0], [1079,2159]];  // [y, x]
    var image = L.imageOverlay('officemap.jpeg', bounds).addTo(map);

    map.setView( [539,1075], 0);   // ([y , x] , zoom)

    map.fitBounds(bounds);

    //L.control.scale('topleft', '50', 'True', 'False', 'True' ).addTo(map); // doesn't work !!!

    L.control.scale().addTo(map);


    var yx = L.latLng;

    var xy = function(x, y) {
      if (L.Util.isArray(x)) {    // When doing xy([x, y]);
          return yx(x[1], x[0]);
    }
    return yx(y, x);  // When doing xy(x, y);
  };

  function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;  
  }

  var sol      = xy(1079, 539);
  //var mizar    = xy(2159, 1079);
  //var mizar    = xy(getRandomIntInclusive(0, 2159), getRandomIntInclusive(0, 1079));
  var kruegerZ = xy(0,  0);

  var maker_text="I am good!!!";

  //var maker_text_array =['Name:', 'Age:', 'Group:'];

  L.marker(sol).addTo(map).bindTooltip(      'Sol');


  L.marker(kruegerZ).addTo(map).bindTooltip(maker_text);

  //var previous_maker=null;
  var current_marker=null;

  /*
  function sleep(milliseconds) {
      const date = Date.now();
      let currentDate = null;
      do {
       currentDate = Date.now();
        } while (currentDate - date < milliseconds);
     };

*/ 
  var i="100"; 
  var mizar    = xy(getRandomIntInclusive(0, 2159), getRandomIntInclusive(0, 1079));
  current_marker=L.marker(mizar).addTo(map).bindTooltip(i);


function del_add(){

  alter("in set");
  map.removeLayer(current_marker);
  mizar= xy(getRandomIntInclusive(0, 2159), getRandomIntInclusive(0, 1079));
  current_marker=L.marker(mizar).addTo(map).bindTooltip("I am OK");
};

//for(var j=0; j<10; j++){

  setTimeout("del_add()", 100);


//};
//alert("done");
//  var travel = L.polyline([sol, deneb]).addTo(map);




</script>
corydon
  • 9
  • 5

1 Answers1

-1

Change

setTimeout("del_add()", 100);

To

setTimeout(del_add, 100);

Firstly setTimetout takes two arguments, first one is function (not a string as in your example) and second one is time in ms.

If you just removed the quotes it would immediately trigger the function. It would be equivalent to this:

setTimetou(det_all(), 100);
// Equivalent to:   
const foo = det_all();
setTimetou(foo, 100);

Wheres if you remove the round brackets you get equivalent to this

setTimetou(det_all, 100);   
// Equivalent to:
setTimeout(() => det_all(), 100);

And this is what you need!

As I said setTimeout takes fucntion. If you wrinte foo() you immediately invoke the function and with combination with setTimeout(foo()) you are passing the result of the function to the setTimetou instead of the function itself

  • `setTimeout` can, in fact, take a string as first parameter. See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout and https://stackoverflow.com/questions/34928853/running-string-as-function-in-javascript-settimeout – IvanSanchez Mar 14 '21 at 16:07