2012-02-14 9 views
0

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

を動作していないがYYYYMMDDHHMMSSから時刻の形式を取り、Just a moment agoのような読みやすいものに変換する関数を拡張したり2 hours ago私はそれを作った前に私のコードは完全に働きました拡張機能に変換する。

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

私はすべての<time>要素からdatetime属性をつかみ、そして戻ってdata-tooltipに古いテキストを設定いただきまして、テキストを切り替えチェーンでその関数を呼び出します。

そのおそらく最高のあなたはjsfiddleページチェックアウトするので、多くのコードがあります:うん

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 

と基本的に私は何見当もつかない:jsLintは次のエラーを返して

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

をこれについては

お時間をお待ちしております。

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

PS - 偶然そこに古いコードを置くには...すべてのあなたの助け

おかげで今のところ.. jsLintは、もはやすべてのエラーを返しているが、そのが正しく実行されていないリンクを変更しません。帰国後

+2

あなたのコードは、jQueryの動作を深く誤解しています。 jQuery Chaining:http://tobiasahlin.com/blog/quick-guide-chaining-in-jquery/を読んで、プラグインオーサリングのドキュメントをhttp://docs.jquery.com/Plugins/Authoringに再読することをお勧めします。 – Shad

+0

ここにありますあなたのコードには構文エラーはありません: http://jsfiddle.net/andromedado/DsTGu/ (そこにはまだ他の種類のエラーがあります) – Shad

答えて

2

一部が間違っ

return returning; 
    }; 
}); 

})(jQuery); 

あり、それは読んでください:

return returning; 
    }; 
}(jQuery)); 

をあなたの構文をチェックするJsfiddleでJSLintボタンを試してみてください。

(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); 
    }); 

})(); 
+0

ありがとう!今私は未知のTypeErrorを取得しています:http://jsfiddle.net/AVLzH/13/ –

+0

これは、jQueryオブジェクトではなく文字列に対してメソッドを呼び出すためです。 – ustun

+0

上記の編集をご覧ください。 – ustun

関連する問題