Note that there are some explanatory texts on larger screens.

plurals
  1. POexecute a function only once
    primarykey
    data
    text
    <p>I have a function which executes on click event, but the thing is that I want it to execute only once. </p> <p>The function that get's executed on click represents a google map plotting to an targeted element. The function looks like this : </p> <pre><code>Cluster.prototype.initiate_map_assembling = function(target, latitude, longitude) { var canvas = $(target).children(); var coordinates = new google.maps.LatLng(latitude, longitude); var options = { zoom: 9, center: coordinates, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map($(canvas)[0], options); var marker = new google.maps.Marker({ position: coordinates, map: map }); }; </code></pre> <p>And I'm running it on <code>click</code> event like this :</p> <pre><code>Cluster.prototype.initiate_google_maps_action = function() { var self = this; return $(this.maps_wrapper_class).each(function(index, element) { var canvas = $(element).parents().eq(3).find(self.map_canvas_wrapper_class); return $(element).on('click', function(ev) { var latitude = $(element).attr('data-latitude'); var longitude = $(element).attr('data-longitude'); self.initiate_map_assembling(canvas, latitude, longitude); ($(canvas).hasClass('selected')) ? $(canvas).removeClass('selected') : $(canvas).addClass('selected'); ev.preventDefault(); }); }); }; </code></pre> <p>What I want to achieve is stop the plotting each time I click the button, because it's only needed once since I'm only hiding the container div and not destroying it. So how could I do that ? I tried with temporary variables added when the function executes ( and setting it as true after the first time and as false when initiated ) and returning false if the temporary variable is true, but with little success as I could not return false inside the plotting function.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload