Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实时更改javascript变量_Javascript_Jquery_Wordpress - Fatal编程技术网

实时更改javascript变量

实时更改javascript变量,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,我有一个视频网站,在源代码中有一个名为ajax_switch.post_status的javascript变量,它有两个值,开或关 我还有一个滑动按钮,有两个开关选项。此按钮可打开或关闭“发布到facebook”。所以当我在10秒钟后按play播放一段视频时,我会将其发布到facebook上。该按钮是一个jquery按钮,所以当我按下on或off时,会在数据库中进行更新。如何在源代码中实时更改ajax_switch.post_status的变量,以便用户访问视频页面并在不刷新的情况下停用face

我有一个视频网站,在源代码中有一个名为ajax_switch.post_status的javascript变量,它有两个值,开或关

我还有一个滑动按钮,有两个开关选项。此按钮可打开或关闭“发布到facebook”。所以当我在10秒钟后按play播放一段视频时,我会将其发布到facebook上。该按钮是一个jquery按钮,所以当我按下on或off时,会在数据库中进行更新。如何在源代码中实时更改ajax_switch.post_status的变量,以便用户访问视频页面并在不刷新的情况下停用facebook帖子,然后单击“播放”不发布

我在wordpress中使用这个。我的职能是:

在单个php中显示按钮代码

<div class="left" id="1"></div>
<div id="ajax" style="padding-left: 10px; line-height: 13px; height: 27px; vertical-align:middle; display: table-cell;" >
    Permiteți Video Best să distribuie acest video pe Facebook cu acordul dvs. Debifând această casetă va opri distribuirea și va șterge postarea de pe cronologia dvs.
</div>
<div class="clear"></div>

<script type="text/javascript">

    $('#1').iphoneSwitch(ajax_switch.post_status, function() 
    {
        $.post(ajax_switch.templateUrl+'on.php', function(data) 
        {
            $('#ajax').html(data);
            ajax_switch.post_status = 'on';
        });
    },
    function() 
    {
        $.post(ajax_switch.templateUrl+'off.php', function(data) 
        {
            $('#ajax').html(data);
            ajax_switch.post_status = 'off';
        });
    },
    {
        switch_on_container_path: ajax_switch.templateUrl+'iphone_switch_container_off.png'
    });
</script>

Permiteți Video Best săDistributie acest Video pe Facebook cu acordul dvs。Debif–and aceastăva opri Distributibuireși vașterge Postrea de pe cronologia dvs。
$('#1').iphoneSwitch(ajax#u switch.post_状态,函数()
{
$.post(ajax\u switch.templateUrl+'on.php',函数(数据)
{
$('#ajax').html(数据);
ajax_switch.post_status='on';
});
},
函数()
{
$.post(ajax\u switch.templateUrl+'off.php',函数(数据)
{
$('#ajax').html(数据);
ajax_switch.post_status='off';
});
},
{
打开容器路径:ajax\u switch.templateUrl+'iphone\u switch\u container\u off.png'
});
以及投寄的条件:

if(ajax_switch.post_status == 'on') 
{
    FB.api(
        '/me/video.watches',
        'post',
        { 
            'movie': '<?php  the_permalink() ?>?ex=257957417674441',
            'access_token': '<?php echo $token; ?>' 
        },
        function(response) 
        {
            if (!response || response.error) 
            {
                console.log(response.error);
            } 
            else 
            {
                console.log('Ewww!');
            }
        });
        }
if(ajax\u switch.post\u status=='on')
{
FB.api(
“/me/video.watchs”,
"岗位",,
{ 
“电影”:“ex=257957417674441”,
“访问令牌”:“
},
功能(响应)
{
如果(!response | | response.error)
{
console.log(response.error);
} 
其他的
{
console.log('ewwww!');
}
});
}

我不明白“iphoneSwitch”方法的作用。我希望有这样的情况:

$("#1").click(function(){
  var targetString = ajax_switch.post_status;
  $.post(ajax_switch.templateUrl + targetString + '.php', function(data) {
    $('#ajax').html(data);   
    ajax_switch.post_status = ajax_switch.post_status == 'off' ? "on" : "off";
  });
});

如果问题不止这些,请告诉我。

由于您目前编写了代码,您必须等待ajax请求响应,然后才能更新
ajax开关。post状态

将代码移到ajax回调之外将允许它立即执行操作,而不会延迟数据库


不过要记住的是,您可能希望在回调中保留一些代码,以便在前端客户机上维护数据库的正确状态。您可以假设更新成功,但您可能希望在回调内部进行检查,以防失败。这样,您的客户端永远不会与数据库不同步。

我想您可以移动
ajax\u开关。post\u status='on'在ajax回调之外。也许我不理解你的问题。这太完美了,我认为这不管用,我也没有试过,但是管用。你可以回答这个问题给你投票。是的,我完全是这样做的:
$.post(ajax\u switch.templateUrl+'on.php',函数(数据){$('ajax').html(数据);ajax\u switch.post\u status='on';})从这里我得到了脚本:。特拉维斯给了我正确的答案,我进行了测试和工作。