Simplest possible irrevocable timout / cooldown function? - javascript

I'm trying to add a 1 second cooldown to my send-message system (as in, you can send 1 message per second max). So my initial thought was simply to create a timeout, and before attempting in sending to check if it exists still. That turned out to take more line of code than I anticipated initially.
Is there something I'm missing here? Isn't there something as simple as:
//inside some message sending function
if(!mySuperCooldown)
{
//send message
mySuperCooldown = cooldown(1000);
}
Everything else I construct successfully ends up taking loads of lines, and it appears to me as something someone thought of before. Thank you, and excuse my illiteracy.

Have a flag that allows messages, and set it to false when a message is sent. Then set a timeout for 1000 milliseconds that resets the flag to true.
var allowMessage = true;
function sendMessage(msg) {
if (allowMessage) {
//do something
allowMessage = false;
setTimeout(() => allowMessage = true, 1000);
}
}

Make a higher order function that turns a normal function into one that is rate limited:
function rate_limit(delay, func) {
var last_call = null;
return function() {
if (last_call && (Date.now() - last_call <= delay)) {
return;
}
last_call = Date.now();
return func();
};
}
You can then rate limit any function:
var my_function = rate_limit(1000, function() {
console.log('foo');
});
Running my_function() will only call your original function once per second.

Related

JavaScript while loop + setInterval doesn't work in Django

I'm developing a website using Django. I have about 50 functions. And it takes about 1 minute to get the result for all of them.
The problem is that I use heroku for my app. And heroku has a limit of 30 sec to get the request. I was suggested to use background tasks. Now I am using background jobs in Python RQ. For each function I made a separate task in Views. Here is an example of one of the tasks:
task_robots_url = q.enqueue(Robots(url).get_url)
robots_url = task_robots_url.result
And now I need to check whether the job is finished in the Template. And when it's finished, display the result.
I want to check with JS each second whether the job is finished. If it's not finished - show "Loading ... " message to the user. If it's finished - show the result.
Here is my JS code:
<script>
var h1 = document.getElementById("h1");
var job_result = "{{ robots_url }}"
var test = function () {
if (job_result == "") {
h1.innerText = "Waiting...";
} else if (job_result == "None") {
h1.innerText = "Waiting ...";
} else if (job_result == undefined) {
h1.innerText = "Waiting ...";
} else {
clearInterval(inter);
h1.innerText = job_result;
}
}
var inter = setInterval(test, 1000);
</script>
But this doesn't work. When the result page starts - I've got an empty result. And it displays Waiting. Then I have None result. And it still displays Waiting. But now after waiting there is no result.
In the documentation for Python RQ it is said I should put time.sleep() for several seconds in my code. And if I put time.sleep(2) - I got the result. But if I put time.sleep(2) for each of my 50 functions - I will get 100 sec. And the limit in Heroku is only 30 sec... So I need to check and display the result without sleep()...
The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.
You are calling setInterval so many times which cause your browser to crash eventually.
assuming job_result value changes after 10 sec you can write the following code:
var job_result = {{ job_result }};
var loadingMessage = function (){
if (!job_result) {
document.write("Loading ...");
}
else {
return
}
}
setInterval(() => loadingMessage, 1000);
You can call return to stop the Interval for running or you can use setTimeout and to call a function that clears the interval

Execute functions once for all Threads

Logger
function called by multiple threads(virtual users).I want to execute
function printDebugLogs(debugLogsRepo) and printResponseCodeRepo(responseCodeRepo) only after given duration passed i.e when IsElapsedTime
return true.Currently all threads executes this functions many times.
//Logger function execute by multithreads
var debugLogsRepo = []
var responseCodeRepo=new Map();
var duration;
var startTime=new Date().getSeconds();
export function Logger(url, request, response, reqFrom, conf) {
//If logging enable
if (conf.logging) {
//ClienSide logging enable
if (conf.clientSideLog) {
//If request failed
pushFailedRequest(url, request, response, reqFrom, debugLogsRepo);
}
//Insert all response codes(i.e pass and failed)
pushResponseCodeStats(response, responseCodeRepo)
//Condition based on which flush logs
if ((IsTimeElapsed(conf))) {
printDebugLogs(debugLogsRepo);
printResponseCodeRepo(responseCodeRepo)
}
}
}
//If duration has been passed
export function IsTimeElapsed(conf) {
var duration = conf.logInterval;
var currentTime = new Date().getSeconds();
if ((Number(startTime) + Number(duration)) <= currentTime) {
startTime = new Date().getSeconds();
return true
}
return false;
}
Each VU in k6 is an independently running JavaScript runtime, potentially on separate machines even, so you can't synchronize such things between VUs.
If you're debugging things, you can simply run your script with only one VU. Or, if for some reason you need to run multiple VUs while you debug, you can print your debug logs in just a single VU by checking the __VU execution context variable:
if (__VU == 1) {
// print logs
}

jQuery prevent reload if button clicked

I have a jQuery datatable that immediately loads ON READY. After that, the datatable is reloaded every 30 seconds. This feature is functioning properly.
I have added a search feature that automatically reloads the datatable with new search results. This part is also functioning properly.
The problem I am experiencing is when I am using the search feature, and the new search results are returned. After 30 seconds, the new results are cleared and the datatable reloads with all of the original records.
Here is what I am currently attempting:
$(document).ready(function()
{
var searchCriteria = "";
displayBookings(searchCriteria);
var idle = 0;
var idleInterval = setInterval(timer, 30000);
$(this).mousemove(function(e){idle = 0;});
$(this).keypress(function(e){idle = 0;});
function timer()
{
idle = idle + 1;
if(idle > 2)
{
displayBookings(searchCriteria);
console.log('table reloaded');
}
}
$('#searchPending').on('click', function()
{
var isPending = 'Y';
var searchCriteria = {
isPending: isPending
};
displayBookings(searchCriteria);
});
});
The function displayBookings() takes searchCriteria. If searchCriteria is blank, then a basic query is fired. Obviously is searchCriteria contains parameters, then the same query is fired with a WHERE clause attached. I did not disclose the code for displayBookings().
All I need to do is stop the 30 second interval if the #searchPending button is clicked.
Clear the interval so it will stop loading.
clearInterval(idleInterval)
specifically in your code:
$('#searchPending').on('click', function()
{
clearInterval(idleInterval)
var isPending = 'Y';
var searchCriteria = {
isPending: isPending
};
displayBookings(searchCriteria);
});
Rather than start and stop the timer interval, since you'll run into a bit of a race condition, you can just have the "refresh" (your "timer" function) refresh using the latest search criteria. To do this, just pass the same object into your displayBookings function. E.g.
const search = { criteria: "" };
$(...).click(() => {
search.criteria = 'change it...';
displayBookings(search.criteria);
});
setInterval(() => displayBookings(search.criteria), 30000);
This way, if a refresh happens, it will use the latest search.criteria. You can achieve the same result with minimal change in your code by simply removing the var from the second searchCriteria. Currently, without removing the var, your outer criteria is being "shadowed" by your inner.
I alluded to debouncing1 in one of my comments. I misread the code and debouncing is not what you want. Instead, you want to only "refresh" if there hasn't been any user activity within some threshold. Here's an alternative from the approach you used:
let lastInteraction = 0;
function interact() {
lastInteraction = Date.now();
}
$(this).mousemove(interact);
$(this).keypress(interact);
Then in your refresh function:
if (Date.now() - lastInteraction > threshold) { ...
Implementing both the central criteria and revised idle check:
$(document).ready(function() {
const idle = {
threshold: 1000,
lastInteraction: 0,
interact() {
idle.lastInteraction = Date.now();
},
isIdle() {
return Date.now() - idle.lastInteraction > idle.threshold;
}
};
const search = { criteria: "" };
$(this).mousemove(idle.interact);
$(this).keypress(idle.interact);
setInterval(() => {
if (idle.isIdle()) {
displayBookings(search.criteria);
}
}, 30000);
$('#searchPending').on('click', () => {
search.criteria = { isPending: 'Y' };
displayBookings(search.criteria);
});
displayBookings(search.criteria);
});
1 The Wikipedia article linked to discusses debouncing with a keyboard. It's the same concept. You'd use debouncing on your displayBookings function if you plan on having it execute live as the user is typing. This would prevent too many HTTP requests from happening in a short duration of time.

What is the error with this tampermonkey code?

My goal is to go http://quizlet.com/12039115/scatter and get a score under 2 seconds. My plan is to do this by disabling the timer with setInterval/clearInterval.
I took some code off some site and tried adapting it to my purposes; it failed. Now I need to know what went wrong.
The original code can be found at blog.jazzychad.net/2011/03/20/inspect-javascript-timers-greasemonkey.html. When I loaded this to Tampermonkey and ran it on the page, only setInterval printed out(multiple times):
INSPECT_TIMERS: setInterval - 100ms
quizlib.2X5g7.js:340
INSPECT_TIMERS: function (){return c.apply(b,a||arguments)}
Thus, I can see that it finds the timer id. Now i need to clearInterval(). Here's where stuff goes wrong.
Code that gave output above:
var go = function(window){
var oldSetInterval = window.setInterval;
var newSetInterval = function(f,t) {
__log("INSPECT_TIMERS: setInterval - " + t + "ms");
__log("INSPECT_TIMERS: " + f);
var id = oldSetInterval(f,t);
return id;
};
window.setInterval = newSetInterval;
//setTimeoutDeleted
function __log(msg) {
if (window.console && window.console.log) {
window.console.log(msg);
}
}
};
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + go + ')(window);';
document.body.appendChild(script); // run the script
When I add
clearInterval(id);
immediately before
return id;
the page literally fails to respond to the click to start the "game". Am I approaching this wrong? Do I need some sort of delay, or am I missing the big picture?
You problem is, there are multiple setInterval calls, looks like 3 on my end.
If you run this code in your console before clicking "Start Game", it will log the following calls to setInterval.
var originalSetInterval = window.setInterval;
window.setInterval = function(func, intr) {
var id = originalSetInterval.apply(window, arguments);
console.log('----setInterval----');
console.log('function:', func);
console.log('interval:', intr);
console.log('id:', id);
console.log('-------------------');
return id;
};
Then when you click "Start Game", you will get output like the following.
----setInterval----
function: function()
interval: 17
id: 10
-------------------
----setInterval----
function: function()
interval: 17
id: 12
-------------------
----setInterval----
function: function()
interval: 100
id: 13
-------------------
Feel free to stop reading here and do some experimenting on your own before continuing to read.
You probably don't want to call clearInterval on all of these. The one that runs the clock appears to be the one with the 100 interval. To disable that interval without touching the other intervals, you can use a simple if statement.
var originalSetInterval = window.setInterval;
window.setInterval = function(func, intr) {
var id = originalSetInterval.apply(window, arguments);
console.log('----setInterval----');
console.log('function:', func);
console.log('interval:', intr);
console.log('id:', id);
console.log('-------------------');
if (intr === 100) {
clearInterval(id);
}
return id;
};
And doing this will successfully stop the clock. However, once you finish the game you will find that the game will still know how long you took. The clock is just a visual element.
If you want to cheat the game, you will need to target the code that actually calculates your final score. Sounds like a great opportunity to learn how to use your browser's developer tools, especially the JavaScript debugger (use the pretty-print feature to make the minified JS easier to read).

Rate limiting to prevent malicious behavior in ExpressJS

Someone made me aware of some flaws in an application I'm working on (mostly within my JavaScript on the front-end), that leaves open the possibility of, say, clicking a ton of buttons at once and sending out a ton of transactional emails. This is clearly not good.
I think one way to handle this in ExpressJS is by using app.all() to count the number of requests that happen within a certain timeframe. I'd store this in the session metadata with timestamps, and if more than X requests happen in Y time, I cut them off for awhile until the limit expires.
Has anyone done this before or have any tips/hints to help me out? Something that's easy to drop in and out of my app is preferable. Thanks!
You could use the Collate object in your webpage.
function Collate(timeout) {
this.timeout = timeout || 1000;
}
Collate.prototype = {
time: 0,
idle: function() {
var t = new Date().getTime();
return (t - this.time > this.timeout && (this.time = t));
},
prefer: function(func) {
this.func = func;
clearTimeout(this.timer);
this.timer = setTimeout(func, this.timeout);
}
};
If you want a function to run once and not run again within the next 1 second.
Like if you want to prevent the user from submitting a form many times, you do this:
var timer = new Collate(3000); //3 seconds
button1.onclick = function() {
if(timer.idle()) {
button1.form.submit();
} else alert("Don't click too quickly!");
}
//or on the form tag
<script>var submitTimer = new Collate(3000);</script>
<form action="post" onsubmit="return submitTimer.idle();">
If you expect an event to fire multiple times and only want to react to the last time it fires.
Like if you want to search after a user has finished typing, you do this:
var timer = new Collate(700); //0.7 seconds
textfield1.onkeyup = function() {
timer.prefer(function() {
autocomplete.search(textfield1.value);
});
};

Categories

Resources