带API调用的同步JavaScript

时间:2017-11-26 10:45:06

标签: javascript

const socketToken = ''; //Socket token from /socket/token end point //This token will only allow one to have access to incoming alerts NOTHING else
var check = 1;
//Connect to socket
const streamlabs = io('https://sockets.streamlabs.com?token=' + socketToken);
var scheck = 0;
//Perform Action on event

streamlabs.on('event', (eventData) => {
    if (eventData.for === 'twitch_account') {
        switch(eventData.type) {
            case 'host':
                var patt = /(\w+)/g
                var text = JSON.stringify(eventData.message);
                console.log(text);  
                var res = text.split(",");
                console.log(res);
                var hoster = res[0];
                hoster = hoster.slice(10, -1);
                var viewers = res[2];
                viewers = viewers.substr(10);
                console.log(hoster + " " + viewers);
                $('#host').promise().done(function( arg1 ) {
                    AnimateHost(hoster,viewers);
                });
                break;
            default:
                //default case
                console.log(eventData.message);
                break;

        }
    }
});

function AnimateHost(name,viewer){
    document.getElementById("hostname").innerHTML = name;
    document.getElementById("hostviewers").innerHTML = viewer;
    $('#host').show();
    $('#host').addClass('animated slideInDown');
    $('#host').one('webkitAnimationEnd', function() {
        setTimeout(function() {
            $('#host').removeClass('slideInDown');
            $('#host').addClass('rotateOutUpRight');
            setTimeout(function() {
                $('#host').removeClass('rotateOutUpRight');
                $('#host').hide();
            }, 1000);   
        }, 5000);
    });
}

所以这行代码可以随时触发。我想要做的是,如果它已经在运行过程中触发,等待上一次函数调用完成后。

0 个答案:

没有答案