Prevent Eventhandler loop based on an event in javascript - javascript

I have javascript code as follows..by which when a client clicks on call button a signal is send to another client and a sound is played at other client till he accepts call. The button turns to End call after clicking Call button:
function beginCall(obj) {
console.log(obj.value);
obj.setAttribute("onclick", "endCall(this,'" + obj.value + "')");
obj.value = 'End Call';
_stream = _streams[obj.id.replace("btn_", "")];
var streamNames = _stream.name.toString();
session.signal({
type: "begincall",
to: _stream.connection,
data: { streamId: _selfstream.streamId + "|" + _selfstream.name }
}, function (error) {
if (error) {
console.log("signal error: " + error.reason);
} else {
console.log("signal sent: begincall:");
}
});
}
function signalEventHandler(event) {
if (event.type == "signal:begincall") {
//sound alert
var audio = new Audio('inc/sound.mp3');
audio.loop = true;
audio.play();
}
}
The issue is that after clicking on call button and if the same client clicks on end call button again and again before the other person accepts the call, multiple audio gets played as loop.
I need to make the audio play only once based on
if (event.type == "signal:begincall") {
How is that possible?

Related

How to play sound with alert message at the same time in React

So I want to play sound with alert message at the same time. When I put alert function and audio.play() function in one functions and call it in onClik() button, sound is being played after you click on "ok" button of pop up alert.
So how can I make them to be executed at the same time?
MY code is below:
_onNextPage() {
const newPageNum = Math.min(
this.state.pageNum + 1,
this.state.content.maxPageNum - 1);
if (newPageNum === this.state.content.maxPageNum - 1) {
const audio = new Audio();
audio.play();
alert("Last page");
return;
}
this.setState({
pageNum: newPageNum,
});
if (this.isTeacher || this.isPreUser) {
this._onUpdatePageNumForTeacher(newPageNum);
}
}

Document.addEventListener() keyup and keydown don't work after click

I'm writing a tampermonkey script that I want to perform an action when a certain set of keys is pressed (cmd + alt + t). I'm seeing a problem where the code stops executing altogether after I click anywhere on the page and I have to either hard refresh or click a certain place on the page (search bar in CloudWatch) to get it working again.
Here's my code:
(async function() {
var set = new Set();
function funcA() {
console.log('funcA');
}
function funcB() {
console.log('funcB');
if (set.has(91) && set.has(18) && set.has(84)) { // cmd + alt + t
funcA();
}
}
function onKeyUp(e) {
console.log('onKeyUp');
set.clear();
console.log(new Array(...set).join(' '));
}
function onKeyDown(e) {
console.log('onKeyDown');
set.add(e.which);
console.log(new Array(...set).join(' '));
funcB();
}
function init() {
console.log('init');
console.log('Adding event listeners.');
document.addEventListener('keyup', onKeyUp);
document.addEventListener('keydown', onKeyDown);
}
if( document.readyState !== 'loading' ) {
console.log( 'not loading' );
init();
} else {
document.addEventListener('DOMContentLoaded', init);
}
})();
The problem only seems to happen on certain pages, for example in AWS CloudWatch logs.
Any ideas?

testing 2-player socket.io game. updating a label is affecting both pages

I don't understand why updating a label on one page is affecting the label on another page. I did not think the DOM was shared like that. Opening one tab or page successfully updates the label to 'player1', but when I open another tab/pg, it updates both labels to 'player2'.
<script>
var socket = io.connect('http://localhost:3000');
socket.on('connect', function() {
socket.emit('join');
socket.on('joinSuccess', function (playerSlot) {
if (playerSlot === 'player1') {
$("#playerID").text("you are player1");
} else if (playerSlot === 'player2') {
$("#playerID").text("you are player2");
}
}); //end joinSuccess
}); //end connect
I am merely trying to notify the user which player they are.
solution:
else if (playerSlot === 'player2') {
var elm = $("#playerID");
var empty = !elm.text().trim();
if (empty) {
elm.text("you are " + playerSlot);
}
}
Are you pushing the 'joinSuccess' message when new user joins? In such case this message will be passed to both the pages with same playerSlot value. So, all pages will be updated last joined player name.
In such case you can handle this with simple condition,
socket.on('joinSuccess', function (playerSlot) {
var elm = $("#playerID");
if (!elm.text().trim()) {
elm.text("you are " + playerSlot);
}
});

How to undo the action Day Pilot.Scheduler on event moved on the last event i saved without reloading the page?

How do I undo the action DayPilot.Scheduler on event moved on the last event I saved without reloading the page?
if (response.result == "OK") {
if (confirm('Are you sure you want to edit this?')) {
DayPilot.request("backend_move.php",
function(req) { // success
var response = eval("(" +req.responseText + ")");
if (response && response.result) {dp.message("Moved: " +response.message);
}
},
args,
function(req) { // error
dp.message("Saving failed");
}
);
//loadEvents();
} else {
args.preventDefault();
}
}

Push notifications ti.App resume event handler (titanium)

Here is my code
function registerPushNotifications() {
Titanium.Network.registerForPushNotifications({
types : [Titanium.Network.NOTIFICATION_TYPE_BADGE, Titanium.Network.NOTIFICATION_TYPE_ALERT],
success : function(e) {
var deviceToken = e.deviceToken;
Ti.API.info("Push notification device token is: " + deviceToken);
Ti.API.info("Push notification types: " + Titanium.Network.remoteNotificationTypes);
Ti.API.info("Push notification enabled: " + Titanium.Network.remoteNotificationsEnabled);
Ti.API.error("device Token is: " + e.deviceToken);
//return device Token to store in Model.
return e.deviceToken;
},
error : function(e) {
Ti.API.info("Error during registration: " + e.error);
},
callback : function(e) {
// called when a push notification is received.
//var data = JSON.parse(e.data);
var data = e.data;
var badge = data.badge;
if (badge > 0) {
Titanium.UI.iPhone.appBadge = badge;
}
var message = data.message;
if (message != '') {
var my_alert = Ti.UI.createAlertDialog({
title : '',
message : message
});
my_alert.show();
}
Ti.App.addEventListener('resume', function() {
alert('do another event if coming from background');
}
});
};
Depending on whether the push notification comes from the background or foreground I want to run different events.
I have tried
Ti.App.addEventListener('resume', function() {
but this fires EVERYTIME I return to the app from the background (the event handlers will be stacked every time a push notification to sent and fires all of them). As opposed to only executing that part of the callback code for push notifications that have come in from the background.
How do I know if the push notification came from the background or whilst app is running? cheers
Update:
Solved it.
If you check the call back object, you will find IsBackground as a property to check where the push notification came from.
Solution:
Check JSON object of callback for the isBackground boolean event.
If 1 it means that it was called from the background, if 0, this means that it wasn't.

Categories

Resources