how to call a function continously - javascript

I want to be able to run this code continuously, so that the function I called is always checking whether entered is true or false
index.js
io.on('connection', function(socket) {
var entered = require('./main.js').onProximityBoolean; /// i want this to continuously be checking itself
if (entered == true) {
socket.emit('enterProximity');
}
}
main.js
var enter = false;
function onProximityBoolean(enter) {
if (enter === true) {
console.log(enter);
return true;
} else {
return false;
}
}
function isWithinBounds() {
//inside here is sensor code that says if the object comes close
//then enter will be true else enter will be false
//this part accurately redefines the onProximityBoolean
}
module.exports = {
onProximityBoolean: onProximityBoolean(enter)
};

You may want to implement an observer with a setter / getter:
"main.js";
var state=false;
var handlers=[];
module.exports={
get proximityBoolean(){
return state;
},
set proximityBoolean(value){
if(value!==state){
state=value;
handlers.forEach(func=>func(value));
},
observe(func){
handlers.push(func);
}
}
So you can do:
var main = require('./main.js');
io.on('connection', function(socket) {
main.observe(function(entered){
if (entered == true) {
socket.emit('enterProximity');
}
});
}
//trigger it somewhen
main.proximityBoolean=true;
Alternatively use setInterval to check regularily:
var main=require('./main.js');
io.on('connection', function(socket){
setInterval(function() {
var entered = main.onProximityBoolean;
if (entered == true) {
socket.emit('enterProximity');
}
},1000);
});

i think you should use cron jobs . Here the npm repo and example of use

Try to use setInterval() method. You can use a while(true) loop but that slow the code a lot, so what I suggest you to do is:
io.on('connection', function(socket){
setInterval(function(socket) {
var entered = require('./main.js').onProximityBoolean;
if (entered == true) {
socket.emit('enterProximity');
}
},1000);}); //after 1 second the function will be called [1000ms]

Related

How to clear terminal using short cut key in java script

This is GDS command to run on Travelport terminal through API. Every command provide a response and after type clear terminal would be cleaned but same I need using shortcut(ctr+any key)
Command.GDS = {
getFsCallback: function(input, output, response_result) {
setTimeout( function() {
window.scrollTo( {
top: 300000,
behavior: "smooth"
});
}, 1000 );
var gds_provider = $('#gds_provider').val();
var gds_output = build_api_call(gds_provider, input.join(" "));
$('#overlay').fadeOut();
if (input == 'clear') {
return output.clear();
}
if(gds_output == 'Enter Password <br><br>'){
$("#cmdline").attr("type","Password");
}else{
$("#cmdline").attr("type","text");
} this.clear = function() {
outputElement.innerHTML = '';
return this;
};
};
return output.write(gds_output);
return function() {
// Add Dir
Terminal.Filesystem.pwd.getDirectory(input[1], {create: true}, function() {}, Terminal.FilesystemErrorHandler);
};
}
};
This is my js code
this.clear = function() {
outputElement.innerHTML = '';
return this;
};
};
Please any help would be appreciated
I would like to clean terminal using any shortcut key like using(ctr+Q) or any of (ctr+key)
Thanks
You can add a key event listener to the document and detect the combination of the "Ctrl" key and the desired key. If the combination is pressed, you can call the clear function to clear the terminal output
document.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.code === "KeyQ") {
outputElement.innerHTML = '';
}
});
replace "KeyQ" with the desired key code that you want to use as the shortcut

Two user getting control access in socket.io - race condition

Two or more users are notified for an available room.
How I am trying to prevent it, and make it so that only one user will be notified available at a time.
socket.on('Check', function (room) {
io.in(room).clients((error, clients) => {
var hasPush = false;
clients.forEach(function (clientId) {
var client = io.sockets.connected[clientId];
if (client.hasPush) {
hasPush = true;
socket.emit('busy', room);
return;
}
}, this);
if (!hasPush) {
socket.hasPush = true;
socket.emit('available', room);
}
});
});
I have tried to prevent using a shared variable but that didn't work.
rooms = {};
socket.on('check', function (room) {
if (!rooms[room]) {
rooms[room] = true;
}
else {
socket.emit('busy', room);
return;
}
.......
rooms[room] = false;
}
Now I have added two servers with nginx and redis, redis-lock work for me.

Kaazing, AngularJS & BasicChallengeHandler

I'm trying to use the Kaazing Library in our HTML5 client. I already implemented it in a java client and it worked. It seems that there is a problem with the LoginHandler. When I debug the code it ends in a endless loop in the LoginHandler. The line callback(new PasswordAuthentication(usr, pwd)) is called over and over again:
// Configure a Basic Challenge Handler
var basicHandler = new BasicChallengeHandler();
basicHandler.loginHandler = function(callback) {
callback(new PasswordAuthentication(usr, pwd));
}
JmsConnectionProperties jmsProps = new JmsConnectionProperties();
jmsProps.connectionTimeout = 1000000;
jmsProps.reconnectAttemptsMax = -1;
jmsProps.reconnectDelay = 3000;
jmsProps.shutdownDelay = 5000;
console.log("Connect to: " + url);
// Create Connection Factory
jmsConnectionFactory = new JmsConnectionFactory(url, jmsProps);
websocketFactory = jmsConnectionFactory.getWebSocketFactory();
websocketFactory.setChallengeHandler(basicHandler);
// reate Connection future handler for the result
try {
if (connection == null) {
var connectionFuture = jmsConnectionFactory.createConnection( function() {
try {
// never comes to this line!!!
connection = connectionFuture.getValue();
// DO SOME STUFF
} catch (e) {
console.dir(e);
// alert(e.message);
}
});
} else {
try {
connection.close(function() { /* Closed */
});
} finally {
connection = null;
}
}
} catch (ex) {
console.dir(ex);
}
Any help would be very appreciated!
Regards
Angela
Is it possible that the username and password combination is incorrect, so that the client is being re-challenged?

JQuery Terminal term.echo from another function?

I am trying to use: http://terminal.jcubic.pl/
I want to be able to call term.echo from another function to be able to place data inside the terminal, but I can not reference it.
Heres the code below:
jQuery(document).ready(function($) {
var id = 1;
$('body').terminal(function(command, term) {
if (command == 'help') {
term.echo("available commands are mysql, js, test");
} else{
term.echo("entered: " + command);
}
}, {
greetings: "Shell",
onBlur: function() {
return false;
}
});
});
How can I access term.echo externally, so I can from like a button click call term.echo to add in data?
Simplest way is to use global variable as a reference for a term object. In your example that can look like follows:
// global placeholder for term object
var terminal = null;
jQuery(document).ready(function($) {
var id = 1;
$('body').terminal(function(command, term) {
// setting up global reference
terminal = term;
if (command == 'help') {
term.echo("available commands are mysql, js, test");
} else{
term.echo("entered: " + command);
}
}, {
greetings: "Shell",
onBlur: function() {
return false;
}
});
});
The issue with this code is that it will load terminal after the ready event fired on document and you are never sure when that happens.
After document.ready fired you will be able to use terminal.echo(''); anywhere.
The value returned by the terminal is the same object as in interpter parameter (in both cases is jQuery object with additional terminal methods), so you can do something like this:
jQuery(function($) {
var terminal = $('body').terminal(function(command, term) {
if (command == 'help') {
term.echo("available commands are mysql, js, test");
} else{
term.echo("entered: " + command);
}
}, {
greetings: "Shell",
onBlur: function() {
return false;
}
});
terminal.echo("Some text");
});

Code doesn't execute correctly the first time. Next time it does

I made some code to check if a value in the database is null or not.
Here is my code:
var tabel;
var running = false;
function CheckRunning(tabel){
this.tabel = "tabel"+tabel+"";
var db = window.openDatabase(this.tabel, "1.0", this.tabel, 1000000);
db.transaction(checkrunningDB, checkerrorCB);
console.log(this.running);
return this.running;
}
function checkrunningDB(tx) {
tx.executeSql('SELECT max(id), sluttime FROM '+this.tabel, [], checkrunningSuccess, checkerrorCB);
}
function checkrunningSuccess(tx, results) {
if (results.rows.item(0).sluttime != null){
this.running = false;
} else{
this.running = true;
}
}
function checkerrorCB(err) {
this.running = false;
console.log(err);
}
So i made a button, the runs this CheckRunning() function.
When i press it, i get this (each line represents a button click):
false
true
true
true
true
Your database functions are running asynchronously. The callback, checkrunningSuccess, will be run when it is done, but the line after db.transaction(checkrunningDB, checkerrorCB); may execute before then.

Categories

Resources