G +登录按钮回调绑定两次

mui*_*day 7 javascript google-plus

我正在使用G +登录我的网站,我发现了这个问题.

我的网站有工具栏(用Javascript渲染)和G +登录按钮,所以我在工具栏文件中附加了G + Javascript API

[工具栏-notlogin.php]

<script>
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js?onload=render';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

function render () 
{
        var config = {
            "callback": "loginCallback",
            "clientid": "xxxxxxxxx.apps.googleusercontent.com",
            "cookiepolicy": "single_host_origin",
            "requestvisibleactions": "http://schemas.google.com/AddActivity",
            "scope": "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email"
            };

            $('#googleCustomLoginBtn .buttonText').text('Login with Google');
            gapi.signin.render ("gplusLoginBtn", config);
        }
</script>
Run Code Online (Sandbox Code Playgroud)

当工具栏(带有G + JS API)加载时,它调用回调函数render()来渲染G +登录按钮.它工作得非常好,所以我的工具栏变为登录状态(显示用户的个人资料)和G +登录按钮消失(HTML替换).

当用户注销工具栏更改状态以不登录(再次替换HTML)时,它再次调用render()并且登录按钮工作.

但问题是当我这次签到回调函数(loginCallback())两次调用时.似乎回调函数再次绑定.(我尝试不再在第二次调用render(),但登录按钮不起作用.)

有没有办法解决它?谢谢.

Pyt*_*tth 1

这里有点新手,但我记得在某处读到,在某些情况下,您可能会意外地多次调用该函数,因为您将其绑定到多个元素。您是否尝试过在声明事件处理程序之前直接删除它?

这样,如果已经存在它的实例,则在添加另一个实例之前会将其删除。

像这样的东西

$('#randomID').unbind('submit').bind('submit');
Run Code Online (Sandbox Code Playgroud)