TheRealFakeNews
TheRealFakeNews

Reputation: 8153

How to check if a function has been called? jQuery

I've been given a script, which I cannot edit (as a part of an exercise). In the script, there is a function which generates random messages for a set of given [fake] users. (It's supposed to emulate a chat room).

I need to detect when a message has been generated. As of now, the only thing I can think of is using setInterval() and repeatedly checking something like length to see if a new message has been added. However, I would like my chatroom to update as the messages are created so that it looks like it's done in real time (as opposed to only updating when it is noticed by setInterval(). I realize that I can have the interval be very small, but that seems highly inefficient, especially if there are larger gaps between messages.

Is there some way to check if that function which generates messages has been "triggered"?

Upvotes: 1

Views: 13276

Answers (4)

futurematt
futurematt

Reputation: 31

You can use a callback function that will only trigger when the first function (the one that generates random messages) completes.

function generateMessage(param1, function() {
  // generate message
  function2(); // callback function
});

function function2() {
  console.log('message generated!');
}

Upvotes: 0

bigOmega  ツ
bigOmega ツ

Reputation: 371

It's impossible to check if a function has been called if you don't have control over it. But what you CAN do is, override it like this

var callCount = 0
const originalGenerateMessage = generateMessage

generateMessage = function(...args) {
  messageTrigger(...args)
  callCount += 1
  return originalGenerateMessage(...args)
}

function messageTrigger() { /* do something */ }

Upvotes: 4

Sarhanis
Sarhanis

Reputation: 1587

Assuming you can't change the code that calls the message generator, you can update the message-generator function as follows.

Essentially you make the original message-generator function call your own change-register function as well as the original message-generator function.

var original = function () { $("#results").append("<p>Original message-generator function</p>");};
var theCopy = original;
var changeRegister = function() { $("#results").append("<p>New function that registers that a change has been made</p>"); };
var override = function() { theCopy(); changeRegister(); };
original = override;

original();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="results"></div>

Upvotes: 1

guest271314
guest271314

Reputation: 1

You can redefine the original function as a variable having the same name as the original function; return the original function when new variable is called within $.when(), use .then() to log when the original function is called

function message() {
  var n = Math.random() * 10000;
  $("body").append("<br>" + n)
}
// set `message` as a variable that returns original `message` function
var messasge = function() {
  return $.when(message()).then(function() {
    // do stuff when `message` called
    console.log("\nmessage called")
  })
}

var count = 0, max = 10, timer;

timer = setInterval(function() {
  if (count !== max) {
    messasge();
    ++count;
  } else {
    clearInterval(timer)
  }
}, 2000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

Upvotes: 0

Related Questions