Question

I am working on a custom script for a room I run on plug.dj, and I have implemented auto message alerts for AFK, working and sleeping. So if you have your status set to AFK and somebody mentions you, they get an auto message back related to the AFK status. here is the code for this :

function autoRespond(data) {
var a = data.type == "mention" && Models.room.data.staff[data.fromID] && Models.room.data.staff[data.fromID] >= Models.user.BOUNCER, b = data.message.indexOf('@') >0;
if (data.type == "mention") {
    if (Models.user.data.status == 1)
    API.sendChat("@" + data.from + " automsg: I'm currently AFK");
    if (Models.user.data.status ==2)
    API.sendChat("@" + data.from + "automsg: I'm currently working");
    if (Models.user.data.status ==3)
    API.sendChat("@" + data.from + " automsg: I'm currently sleeping");
    }

}

What I want to do is, if somebody gets mentioned while they are set as one of the statuses, I want to add a timer before the auto message because active again. say I have mine set to AFK, then somebody mentions me, they will receive an auto message back, then if somebody mentions me within the timer, the auto message is unable to send (not delayed then sent after the delay). I hope I can get the correct help for this, thanks.

Was it helpful?

Solution

you can use setTimeout as follows:

HTML:

<button id="button">message</button>
<div></div>

JavaScript:

$('#button').on('click', function(){
    sendMessage();   
}); 
var sended = false ;
function  sendMessage(){
    if( !sended ){
        $('div').html($('div').html() + '<br />new message');
        sended = true;
        wait( 2000 );
    }
}

function wait( miliseconds ){
     setTimeout(function(){
         sended = false;
     }, miliseconds)   
}

You can see this working at jsfiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top