I have a method that is running multiple times and I'm not sure why. Since this method does a ajax call, I don't want to fetch the same data twice.
'use strict';
var myMethod = (function() {
var lock = false;
return function(){
if (!lock) {
lock = true;
console.log('this code only runs one time');
}
console.log('myMethod doing stuff');
};
})();
myMethod();
myMethod();
'use strict';
var myObjectConstructor = function() {
var myObject,
constructor = function(){
console.log('this stuff only runs one time');
var foo = true,
bar = 'yay';
return {
foo: foo,
bar: bar
};
};
myObject = myObject || constructor();
return myObject;
};
console.log(new myObjectConstructor());
console.log(new myObjectConstructor());
I'm using jQuery and I want to know how to attach a click event listener and remove it when it's done.
'use strict';
var $el = $('body'),
eventHandler = function() {
console.log('click');
$el.unbind('click', eventHandler);
};
$el.bind('click', eventHandler);
I'm using angular and I have to do 3 $http
calls but I'm not sure how can I
make it work.
'use strict';
var promises = [];
promises.push($http.get('url1'));
promises.push($http.get('url2'));
promises.push($http.get('url3'));
Promises.all(promises).then(function(){
console.log('yay');
}, function(){
console.log('awwww man');
});
Is there a way to prevent hoisting from happening and what it is.
var holi = 'holi :D';
function sayHoli(){
console.log(holi);
var holi = 'jelou :v';
console.log(holi);
}
sayHoli();
Scope A local variable can have the same name as a global variable, but it is entirely separate; changing the value of one variable has no effect on the other. Only the local version has meaning inside the function in which it is declared.
'use strict';
var holi = 'holi :D';
function sayHoli(){
console.log(holi);
holi = 'jelou :v';
console.log(holi);
}
sayHoli();
- Join us at the #javascript channel for support.
- Asking is better than failling.
- You are not alone!