Check if the function was made - javascript

So basically i need to check if the est_wont_show was made/fulfilled/executed.
function est_wont_show() {
var HideThis, estpgt_id;
estpgt_id = $(this).attr("estpgt_id");
//Saving in DOM
HideThis = $("#estpgt_" + estpgt_id).detach();
HideThis = $("#estpt_tr_for_" + estpgt_id).detach();
if (document.body.getElementsByTagName(HideThis)) {
//Check if element is detached
alert("Element is in DOM");
}
}
Something in this way(this function is tied up to button)
function TEST_ALERT() {
if () {
//check if function was made
alert('hello');
} else {
alert('NO-2');
}
}
He should check because in the end there will be 2 things. Like if the elements ARE in DOM, then he will delete them, if they are not, then he will bring them back.

You can use a global scoped variable to know if it has been executed.
// We can use a flag to see if the function got executed
let est_wont_show_execution_flag = false;
function est_wont_show() {
// Do something ...
est_wont_show_execution_flag = true;
}
function TEST_ALERT() {
// Did est_wont_show has been executed ?
if (est_wont_show_execution_flag) {
console.log('TEST_ALERT : est_wont_show has been executed');
return;
}
console.log('TEST_ALERT : est_wont_show hasn\'t been executed');
}
TEST_ALERT();
est_wont_show();
TEST_ALERT();

You can use typeof operator to see if any function is available by the given name
function TEST_ALERT() {
if (typeof est_wont_show === "function") {
//check if function was made
alert('hello');
} else {
alert('NO-2');
}
}
If the function is not created typeof est_wont_show will be "undefined"

Related

How should I increase the number of && operators as per the array argument is passed in function?

function ShortCutKey(elemKey, keyCode, func) {
var elemId = document.getElementById(elemKey);
function RunFuncShorcut(enKey, numkeys) {
if (numkeys > 1) {
if (enKey.code == keyCode[0] && enKey.code == keyCode[1]) func();
} else{
if (enKey.code == keyCode[0]) func();
}
}
if (Array.isArray(keyCode)){
if (keyCode.length > 1) {
elemKey.addEventListener("keyup", function() {
RunFuncShorcut(keyCode, 2);
});
} else{
elemKey.addEventListener("keyup", function() {
RunFuncShorcut(keyCode, 1);
});
}
}
else{
throw "2nd Argument must be an Array";
}
}
It is just limited to two keys means only a combination of two keys. I want multiple combination with less code... But I still don't know how!
You can use every
The every() method tests whether all elements in the array pass the
test implemented by the provided function. It returns a Boolean value.
function ShortCutKey(elemKey, keyCodes, func) {
const elemId = document.getElementById(elemKey);
function RunFuncShorcut(enKey) {
if (keyCodes.every(keyCode => keyCode === enKey.code)) func();
}
if (Array.isArray(keyCodes)){
elemKey.addEventListener("keyup", function() {
RunFuncShorcut(keyCodes);
});
}
else{
throw "2nd Argument must be an Array";
}
}

Reducing function name usage within function

When I search for a function name - it gets clumsy when I see plenty of function names as in the example. In large code base I waste my time when searching for how and from where a function has been called :
function do_something()
{
if (typeof do_something.flag == "undefined")
{
do_something.flag = true;
}
if (do_something.flag == null)
{
do_something.flag = true;
}
}
Here when I search for do_something so that I can look from where it is called instead I find plenty of lines consisting of do_something.flag1,do_something.flag2 and so on which isn't of any use in most of such searches. In large function I get plenty of such lines occupying search output.
I've another scenario. In (Netbeans) IDE I want to do Ctrl-F do_something function looking for where it is called in the file. Now I find pressing F3 within the function itself iterating over it's own lines containing something like do_something.var1=5 etc.
In short is there any way to reduce the function name usage within the function when creating object-global variables?
I've much longer functions but I'll give real example of medium level function causing this problem:
function slow_down_clicks(label, userfunc, timeinmsec)
{
console.log("slow_down_clicks=" + label);
if (typeof slow_down_clicks.myobj == UNDEFINED)
{
slow_down_clicks.myobj = {};
}
if (typeof slow_down_clicks.myobj[label] == UNDEFINED)
{
slow_down_clicks.myobj[label] = {
inqueue: false,
lastclickedtime: 0,
login_post_error_count: 0
};
}
var myfunc = function ()
{
console.log("Executing the user func");
slow_down_clicks.myobj[label].inqueue = false;
slow_down_clicks.myobj[label].lastclickedtime = new Date().getTime();
userfunc();
}
console.log("Due to error in home.aspx reloading it", ++slow_down_clicks.myobj[label].login_post_error_count);
if (slow_down_clicks.myobj[label].inqueue == false)
{
var diff = new Date().getTime() - slow_down_clicks.myobj[label].lastclickedtime;
console.log("diff=", diff, timeinmsec);
if (diff > timeinmsec)
{
myfunc(); //click login
}
else
{
console.log("queuing the request after:", timeinmsec - diff);
slow_down_clicks.myobj[label].inqueue = true;
setTimeout(function ()
{
console.log("called myfunc babalatec");
myfunc();
}, timeinmsec - diff);
}
}
else
{
console.log("Discarding this request...");
}
}
I think you can just define the fields as normal variables and put your code in its own file. Then you can just refer to the variables by its name inside your function because they are within the function's closure. The variables will not be accessible outside because you limit them in its own file.
like:
let flag = false
function doSomething () {
if (!flag) {
flag = true
}
....
}
Put the above code snippet in a separate file and import the function when you want to use it.

Simple Javascript: How to invoke a function based off user prompt;

I've defined some functions, and I want to get user input to invoke those functions. I have the following code, but can't figure out how to invoke the actual function when I'm using a variable. I assumed below code would work..
thanks!
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
} else {
return false;
}
}
var getCommand = function(){
var command = prompt("Please enter a command");
if (!(command in someFunctions)) {
alert('command not recognized');
getCommand();
} else {
command();
}
}
getCommand();
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
}
else {
return false;
}
}
};
var getCommand = function(){
var commandInput = prompt("Please enter a command");
var command = someFunctions[commandInput];
if (!command) {
alert('command not recognized');
getCommand();
}
else {
command();
}
};
getCommand();
The reason your code isn't working is because you're missing the closing } of someFunctions.
var someFunctions = {
play: function() {
if (player.stopped()) {
player.play();
} else {
return false;
}
}
} // here
Your call is fine, you call a "variable" function the same way you do a regular one. The only difference between a "variable" function and an ordinary one is you can call the ordinary one before it's declared (if you're in the same scope at least)

best way to toggle between functions in javascript?

I see different topics about the toggle function in jquery, but what is now really the best way to toggle between functions?
Is there maybe some way to do it so i don't have to garbage collect all my toggle scripts?
Some of the examples are:
var first=true;
function toggle() {
if(first) {
first= false;
// function 1
}
else {
first=true;
// function 2
}
}
And
var first=true;
function toggle() {
if(first) {
// function 1
}
else {
// function 2
}
first = !first;
}
And
var first=true;
function toggle() {
(first) ? function_1() : function_2();
first != first;
}
function function_1(){}
function function_2(){}
return an new function
var foo = (function(){
var condition
, body
body = function () {
if(condition){
//thing here
} else {
//other things here
}
}
return body
}())`
Best really depends on the criteria your application demands. This might not be the best way to this is certainly a cute way to do it:
function toggler(a, b) {
var current;
return function() {
current = current === a ? b : a;
current();
}
}
var myToggle = toggler(function_1, function_2);
myToggle(); // executes function_1
myToggle(); // executes function_2
myToggle(); // executes function_1
It's an old question but i'd like to contribute too..
Sometimes in large project i have allot of toggle scripts and use global variables to determine if it is toggled or not. So those variables needs to garbage collect for organizing variables, like if i maybe use the same variable name somehow or things like that
You could try something like this..: (using your first example)
function toggle() {
var self = arguments.callee;
if (self.first === true) {
self.first = false;
// function 1
}
else {
self.first = true;
// function 2
}
}
Without a global variable. I just added the property first to the function scope.
This way can be used the same property name for other toggle functions too.
Warning: arguments.callee is forbidden in 'strict mode'
Otherwise you may directly assign the first property to the function using directly the function name
function toggle() {
if (toggle.first === true) {
toggle.first = false;
// function 1
}
else {
toggle.first = true;
// function 2
}
}

Returning variable from function is undefined once returned

Is there any reason that this:
function find_parent_p(x){
daddy = jQuery(x).parent();
if(daddy.attr("tagName").toLowerCase() == 'p'){
console.log(daddy,"result");
return (daddy);
} else {
find_parent_p(daddy);
}
}
jQuery(document).ready(function($){
$('img').each(function(){
next = find_parent_p($(this));
})
});
would return a jQuery object in the console (expected behaviour), where as the following returns Undefined All I am doing is moving the call to console.log outside the function, and after the call to it:
function find_parent_p(x){
daddy = jQuery(x).parent();
if(daddy.attr("tagName").toLowerCase() == 'p'){
return (daddy);
} else {
find_parent_p(daddy);
}
}
jQuery(document).ready(function($){
$('img').each(function(){
next = find_parent_p($(this));
console.log(next,"result");
})
});
You're missing the return statement in your else condition. If your function recurses, then the top level call won't return anything, and you'll end up with undefined.
else {
return find_parent_p(daddy);
}
I'm not sure if this is causing the problem, but the function only works if it finds the element immediately.
Make daddy a local variable (to prevent possible conflicts with global variables)
Return the result from the recursive call
:
function find_parent_p(x) {
var daddy = jQuery(x).parent();
if(daddy.attr("tagName").toLowerCase() == 'p') {
return daddy;
} else {
return find_parent_p(daddy);
}
}
Note: You can do the same using just jQuery:
var next = $(this).closest('p');
If you aim to get the img's parent which is p, you can use.
$('img').each(function(){
next = $(this).closest('p');
console.log(next,"result");
})

Categories