最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if a function has been called? jQuery - Stack Overflow

programmeradmin3浏览0评论

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"?

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"?

Share Improve this question asked Feb 22, 2016 at 5:06 TheRealFakeNewsTheRealFakeNews 8,20324 gold badges77 silver badges131 bronze badges 3
  • @Ray - which event is triggered by a function being called? – nnnnnn Commented Feb 22, 2016 at 5:17
  • How is the function called? – nnnnnn Commented Feb 22, 2016 at 5:18
  • @nnnnnn There's a timer function that creates a new random interval each time, then it calls the function to create new messages. – TheRealFakeNews Commented Feb 22, 2016 at 5:25
Add a ment  | 

4 Answers 4

Reset to default 4

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 */ }

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./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="results"></div>

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./ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

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

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

function function2() {
  console.log('message generated!');
}
发布评论

评论列表(0)

  1. 暂无评论