开发者

Unix timestamp to seconds in javascript

开发者 https://www.devze.com 2023-03-22 05:25 出处:网络
I\'m trying to do a program which executes after 15 minutes of being in the page. My problem is how to get the exact number to add on the timestamp which is stored in a cookie.

I'm trying to do a program which executes after 15 minutes of being in the page. My problem is how to get the exact number to add on the timestamp which is stored in a cookie.

I need a function to convert seconds into timestamps or anything that can make the action execute after 15 minutes. I don't really know how much time is 1792939 which I place in the code below.

setInterval("timer()",1000);



    $.cookie("tymz", time);

    function timer(){
        var d = new Date();
        var time = d.getTime();

        var x = Number($.开发者_运维技巧cookie("tymz")) + 1792939;

        //alert('Cookie time: ' + x + '\nTime: ' + time);


        if(time > x){
            alert('times up');
        }else{
            //alert('not yet\n' + 'times up: ' + x + '\ntime: ' + time);
        }

    }


How about using setTimeout(..)?

<script type="text/javascript">
function myFunc()
{
    alert("I will show up  15 minutes after this pages loads!");
}
setTimeout("myFunc()",60*15*1000);
</script>

Check this: http://www.w3schools.com/js/js_timing.asp


unix timestamp are second from epoch (1/1/1970) so if you want to execute some code after 15 minutes just record the time when the page is loaded then every second calculate how many seconds are passed from page load. When the difference between current time and page load time is greater than 15*60*1000 you can execute your code.

var pageLoad = new Date().getTime();
function tick(){
    var now = new Date().getTime();
    if((now - pageLoad) > 15*60*1000) executeYourCode();
}
setInterval("tick()",1000);

Remeber that javascript return time in millisecond

Hope this helps


If the number is seconds since 1/1/1970 00:00:00, then you can convert '1792939' to a javascript date by multiplying by 1,000 and passing to Date:

var d = new Date(1792939 * 1000) // Thu Jan 22 1970 04:02:19

Currently it is about 1311428869 seconds since 1/1/1970. So if you have a value for seconds, then you can use setInterval to run a function 15 minutes after that:

var seconds = ?? // set somehow
var start = new Date(seconds * 1000);
var now = new Date();
var limit = 15 * 60 * 1000;
var lag = now - start + limit;

// Only set timeout if start was less than 15 minutes ago
if ( lag > 0 ) {
  setTimeout( someFn, lag);
}

Provided the current time is less than 15 minutes from the start time, the function will run at approximately 15 minutes after the start time. If the system is busy when the time expires, the function should be run as soon as possible afterward (usually within a few ms, but maybe more).


works without server or cookie (and all browser after IE7)

Looks like you use jQuery, so you might as well use jQuery.now() insted

var firstVisit = localStorage['firstVisit'] = localStorage['firstVisit'] || $.now();

function myFunc(){
    alert("I will show up  15 minutes after this pages loads!");
}

setTimeout(myFunc, parseInt(firstVisit) - $.now() + 1000 * 60 * 15);
0

精彩评论

暂无评论...
验证码 换一张
取 消