JavaScript Recycle for loop? recycle loop in chemical engineering,recycle loop experiment,recycle loop

not sure what you meant by "recycle" the loop but that looks like a nicer code. i dont see anything to gain here by combining all the loops together

var count = something.length;
function loopFunc(action){
   for( a = 0; a < count; a++ ) {
        action();
   }
}
function addMapLoopEvent(eventName,action){
   google.maps.event.addDomListener( div, eventName, function( event ) {
        loopFunc(action)
    });
}

addMapLoopEvent('click', action1));
addMapLoopEvent('mouseover', action2));
addMapLoopEvent('mouseout', action3));    
Answer:1

It would only be efficient to combine the loops if they were happening, more or less, at the same time:

for (a=1;a<10;a++) {
   // action a
}
for (b=1;b<10;b++) {
   // action b
}

becomes:

for (a=1;a<10;a++) {
   // action a
   // action b
}

That is not the case here. You have three different events which are mutually exclusive. As long as the actions are very different -- meaning there's not much repeated code -- there is no advantage to combining them. In fact, it would probably be a disadvantage in terms of readability.

Further complicating the problem is that, unlike jQuery's on method, vanilla JS can't pass more than one event type at a time to addDomListener. So you'd have to write a custom loop to split up the event string, then test the event.type to run the appropriate loop, further complicating your code without a clear benefit.

Answer:2

You can do something like this:

var count = something.length;

function customCallback(callback) {
    for( var a = 0; a < count; a++ ) {
        if(typeof callback == 'function') {
            callback();
        }

        console.log(count + ' => ' + a)
    }
}

function someAction(msg) {
    console.log(msg);
}

google.maps.event.addDomListener( div, 'click', function( event ) {
    customCallback(); // will output your console.log from customCallback function
});

google.maps.event.addDomListener( div, 'mouseover', function( event ) {
    customCallback(someAction); // will output your console.log from someAction function (undefined) and customCallback function console.log
    customCallback(someAction('hi')); // will output your console.log from someAction function ("hi") and customCallback function console.log
});

google.maps.event.addDomListener( div, 'mouseout', function( event ) {
    customCallback(function(event) {
        console.log('Do something awesome.');
        event.preventDefault();
    }); // will output your console.log from someAction function (undefined) and customCallback function console.log
});
Answer:3

This isn't much of a problem but I'm wondering if theres a way I can turn this $(".selector").sortable("disable").sortable("refresh"); into this $(".selector").sortable("disable", "refresh"); ...

This isn't much of a problem but I'm wondering if theres a way I can turn this $(".selector").sortable("disable").sortable("refresh"); into this $(".selector").sortable("disable", "refresh"); ...

I'm wanting to swap the second class of Bootstraps 'glyphicon' span, but instead of toggling the class, It's adding it behind, thus not changing the class at all. I'm new(ish) to jQuery / Javascript ...

I'm wanting to swap the second class of Bootstraps 'glyphicon' span, but instead of toggling the class, It's adding it behind, thus not changing the class at all. I'm new(ish) to jQuery / Javascript ...

  1. change bootstrap glyphicon color
  2. change bootstrap glyphicon size
  3. bootstrap change glyphicon onclick
  4. bootstrap change glyphicon on collapse
  5. bootstrap change glyphicon
  6. bootstrap change glyphicon jquery
  7. custom bootstrap glyphicons
  8. bootstrap change glyphicon javascript
  9. bootstrap reduce glyphicon size
  10. css change bootstrap glyphicon color
  11. bootstrap customize glyphicon

I'm almost done with my project, but the specifications for my login is not yet 100% done. Remember me function is not yet working. HTML <input type="text" id="signinId" class="email-input" ...

I'm almost done with my project, but the specifications for my login is not yet 100% done. Remember me function is not yet working. HTML <input type="text" id="signinId" class="email-input" ...

$("body").on('click', 'button', function(e) { var selection = window.getSelection().toString(); alert(selection); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery....

$("body").on('click', 'button', function(e) { var selection = window.getSelection().toString(); alert(selection); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery....