简体   繁体   中英

$.fn.fixTime not working

http://jsfiddle.net/AVLzH/14/

I'm trying to build my first jQuery extended function which will take the time format from YYYYMMDDHHMMSS and convert it to readable things like Just a moment ago or 2 hours ago my code worked perfectly before I made it into an extended function.

Sidebar: yes I know its better to use Server-side code to get the current time, this is just for example purposes

When I call the function its in a chain that grabs the datetime attribute from all <time> elements, and switches the text with whats returned and sets the old text to data-tooltip .

There's a lot of code so its probably best you check out the jsfiddle page:

http://jsfiddle.net/AVLzH/14/

jsLint is returning the following errors:

 Error: Expected an assignment or function call and instead saw an expression. }); Expected '(end)' and instead saw '}'. })( jQuery ); Implied global: jQuery 3, console 19,20,22,23,40,50,75 

Yeah and basically I have no clue what to do about this.

Thanks for any help ahead of time!

http://jsfiddle.net/AVLzH/14/

PS - accidently put old code in there.. changed link

Thanks for all your help so far.. jsLint is no longer returning any errors, but its not executing correctly.

The part after returning is wrong

 return returning;
    };
});

})( jQuery );

, it should read:

  return returning;
    };
}( jQuery ));

Try the JSLint button at Jsfiddle to check your syntax.

(function( $ ) {

    jQuery.fn.fixTime = function(activityTime) {



            var currentTime = new Date(),
                month  = currentTime.getMonth() + 1,
                day    = currentTime.getDate(),
                year   = currentTime.getFullYear(),
                hour   = currentTime.getHours(),
                minute = currentTime.getMinutes(),
                second = currentTime.getSeconds(),
                activityTime = new Date(parseInt(this.attr('datetime'), 10)),
                masterTime = (year*10000000000) + (month*100000000) + (day*1000000) + (hour*10000) + (minute*100) + (second * 1),
                timeDifference = masterTime - activityTime,
                returning;

            console.log("Current Time: " + month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second);    
            console.log("Current Time: " + year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second);

            console.log("Current Time:  " + masterTime);
            console.log("Activity Time: " + activityTime, this);

        console.log(this.attr('datetime'))
            console.log(new Date(20120211103802))
            // Change Time 
            timeDifference = timeDifference + 0;

            // YYYYMMDDHHMMSS

            //             60 - 1 Min
            //           6000 - 1 Hour
            //         240000 - 1 Day
            //        7000000 - 1 Week
            //       30000000 - 1 Month
            //    10000000000 - 1 Year

            // YYYYMMDDHHMMSS

            console.log("Time Difference: " + timeDifference);

            if (0 <= timeDifference && timeDifference < 60) {
                returning = "Just a moment ago";
            } else if (60 <= timeDifference && timeDifference < 120) {
                returning = "A minute ago";
            } else if (120 <= timeDifference && timeDifference < 6000) {
                timeDifference = Math.floor(timeDifference/100);
                returning = timeDifference + " minutes ago";
            } else if (6000 <= timeDifference && timeDifference < 20000) {
                console.log("1 hour ago");
            } else if (20000 <= timeDifference && timeDifference < 240000) {
                timeDifference = Math.floor(timeDifference/10000);
                returning = timeDifference + " hours ago";
            } else if (240000 <= timeDifference && timeDifference < 2000000) {
                returning = "Yesterday";
            } else if (2000000 <= timeDifference && timeDifference < 7000000) {
                timeDifference = Math.floor(timeDifference/1000000);
                returning = timeDifference + " days ago";
            } else if (7000000 <= timeDifference && timeDifference < 14000000) {
                return "A week ago";
            } else if (14000000 <= timeDifference && timeDifference < 30000000) {
                timeDifference = Math.floor(timeDifference/7000000);
                returning = timeDifference + " weeks ago";
            } else if (30000000 <= timeDifference && timeDifference < 200000000) {
                returning = "A month ago";
            } else if (200000000 <= timeDifference && timeDifference < 10000000000) {
                timeDifference = Math.floor(timeDifference/100000000);
                returning = timeDifference + " months ago";
            } else if (10000000000 <= timeDifference && timeDifference < 20000000000) {
                returning = "A year ago";
            } else if (20000000000 <= timeDifference && timeDifference < 1000000000000) {
                timeDifference = Math.floor(timeDifference/10000000000);
                returning = timeDifference + " years ago";
            } else {
                console.error("Error Calculating"); // Only when less than zero or longer than 100 years
                returning = "undefined";
            }

            return returning;
        };

}( jQuery ));

(function() {

    var times = $('time');

    times.each(function() {
        var beforeTime = $(this).text();
   //     var betterTime = new Date($(this).attr('datetime'));

        var betterTime = $(this).fixTime();

        $(this).text(betterTime).attr('data-tooltip', beforeTime);
    });

})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM