4
$(function () {
let check = function() {
    setTimeout(function () {
        if (cLANGUAGE == null)
            check();
    }, 500);
};
if (cLANGUAGE == null) check();
alert(cLANGUAGE);

$('#details--action-share').on('click', function () {
    Action.details.share.before();
});
});

When I write like this, it still alerts cLanguage is null. I want to wait until cLanguage is not null then run below code

$('#details--action-share').on('click', function () {
    Action.details.share.before();
});). 

How can I do this?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Nevermore
  • 1,663
  • 7
  • 30
  • 56
  • _"below code"_ means ? – Pranav C Balan Mar 12 '19 at 09:09
  • $('#details--action-share').on('click', function () { Action.details.share.before(); }); – Nevermore Mar 12 '19 at 09:09
  • Incidentally, do you want to check for [`null` or undefined](https://stackoverflow.com/questions/2703102/typeof-undefined-vs-null)? – showdev Mar 12 '19 at 09:12
  • This might help: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Mar 12 '19 at 09:15
  • Can you clarify: you only want to add the event handler when it's not null? Or you only want the Action.details.share.before() to run when it's not null? Or do you want to be able to click '#details-action-share' and then have .before() run when it's no longer null? – freedomn-m Mar 12 '19 at 09:44
  • I want to wait until the cLanguage value is not null and then initialize #details-action-share click event. If cLanguage is null, dont initialize click event, if cLanguage is not null, initialize click event. – Nevermore Mar 12 '19 at 10:11

2 Answers2

5

Put all the code that depends on it not being null in an else block, in case the === null evaluates to false:

let check = function() {
  setTimeout(function () {
    if (cLANGUAGE === null)
      check();
    else {
      alert(cLANGUGAGE);
    }
  }, 500);
};
check();
Snow
  • 3,820
  • 3
  • 13
  • 39
  • 1
    Why? A timeout is not an interval, it doesn't need to be terminated - once the callback is running, the timeout has finished, it won't run anymore afterwards. – Snow Mar 12 '19 at 09:15
  • 1
    +1 because it's correct, but it's a but untidy. I'd personally say `if (cLANGUAGE === null) { setTimeout(check, 500); } else { blah...` I consider that much more obvious at first glance, but either way you're right :) – Reinstate Monica Cellio Mar 12 '19 at 09:19
  • @freedomn-m Chained asynchronous calls (like recursive `setTImeout`s) won't result in an overflow - only synchronous recursive calls have that issue. – Snow Mar 12 '19 at 09:53
0

You can use true/false boolean like this

$(function () {
   var Checked = false; // set checked here to false
   let check = function() {
    setTimeout(function () {
        if (cLANGUAGE == null){
            check();
        }else{
            Checked = true; // change it here to true
        }
    }, 500);
  };
  if (cLANGUAGE == null) check();
  alert(cLANGUAGE);

  $('#details--action-share').on('click', function () {
     if(Checked === true){ // or just if(checked) to check if its true
        Action.details.share.before();
     }
  });
});
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28