duanlan8763 2017-03-30 08:36
浏览 34
已采纳

Javascript - 带有cookie检查器的延迟按钮

i am trying to do something that it's still out of my league. I have used before script on website to show Button after 1 minute from video time. So when Video player comes to 01:00 the button is shown and after that it saves that to cookie so the next time you visit page it's shown right away and you don't have to wait another 1 minute so button can be shown.

But now i am trying to connect this timer to the website time, not the video time, and i am pretty lost.

Function calling in Index page:

<script type="text/javascript">
var minuten = 0;
var sekunden = 5;

//==========================
cartt(minuten, sekunden);   
</script>

This is the original code of the video time:

 $("#video").fitVids(); 
 var video = new Vimeo.Player($('#video iframe'));
 function cart(minuten, sekunden) {

    var button = $('.cart');
    var seconds = (minuten * 60) + sekunden;
    var url = window.location.pathname;
    url = url.split("/");
    var seite = url[url.length -1];
    var cookie_string = "cartbutton_" + seite + "=show";
    var cookieIsSet = function() { return document.cookie.indexOf(cookie_string) != -1; };

    if(!cookieIsSet()) {
        button.hide();

        video.on('timeupdate', function(data) {
            if (data.seconds >= seconds && !cookieIsSet()) {
                button.show();
                document.cookie = cookie_string + "; max-age=259200";
            }
        });
    }

}

And this is how i am trying to get website time:

window.onload=function(){
      var start=Date.now();

}
function cartt(minuten, sekunden) {

    var button = $('.cartt');
    var seconds = (minuten * 60) + sekunden;
    document.write(seconds);
    var url = window.location.pathname;
    url = url.split("/");
    var seite = url[url.length -1];
    var cookie_string = "cartbutton_" + seite + "=show";
    var cookieIsSet = function() { 
        return document.cookie.indexOf(cookie_string) != -1; 

        };



    if(!cookieIsSet()) {

     start.on(function(data) {
            if (data.seconds >= seconds && !cookieIsSet()) {
                button.show();
                document.cookie = cookie_string + "; max-age=259200";
            }
        });

        button.hide();


    }

}
  • 写回答

1条回答 默认 最新

  • dqs86517 2017-03-30 10:33
    关注

    (Posted on behalf of the OP).

    I have managed to make this by myself, since there are no answers and I couldn't find this answer on the web, I will share my solution.

    On index page:

      <script type="text/javascript">
        var videoShow = getCookie('video-show');
        jQuery(document).ready(function() {
            if(videoShow) {
                jQuery('.cartt').show();
            }
            else {
                setTimeout(
                    function(){
                        jQuery('.cartt').show();
                        setCookie('video-show', 1, 365);
                    }, 5000            );
            }
      });
        </script>
    

    The script:

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    var dpath = '/';
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString())+";path="+dpath;
    }
    
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看