Javascript JS时钟不';在外部JS文件中无法工作
我的问题是,当JS时钟直接放在HTML文档的头部时,它似乎工作正常,但当我从外部.JS文件加载它时,它就无法工作。在我的.js文件中还有很多其他的东西可以使用,但不是这个!你知道问题是什么吗Javascript JS时钟不';在外部JS文件中无法工作,javascript,jsfiddle,Javascript,Jsfiddle,我的问题是,当JS时钟直接放在HTML文档的头部时,它似乎工作正常,但当我从外部.JS文件加载它时,它就无法工作。在我的.js文件中还有很多其他的东西可以使用,但不是这个!你知道问题是什么吗 function startTime() { var today=new Date(), h=today.getHours(), m=today.getMinutes(), s=today.getSeconds(); // add a
function startTime() {
var today=new Date(),
h=today.getHours(),
m=today.getMinutes(),
s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('bigtime').innerHTML=h+":"+m+":"+s;
}
function checkTime(i) {
if (i<10) {
i="0" + i;
}
return i;
}
我将加载jquery库,并在JS文件中执行
这使HTML更干净,并将所有JS保持在一个位置 我将加载jquery库,并在JS文件中执行
这使HTML更干净,并将所有JS保持在一个位置 jsiddle的
onLoad
选项将整个JS包装在//和}/]>
这意味着您的startTime
函数只有在body
上的onload
事件处理程序触发后才会定义
使用No wrap-in
选项修复此问题。jsFiddle的onLoad
选项将整个JS包装在//和}/]>
这意味着您的startTime
函数只有在body
上的onload
事件处理程序触发后才会定义
使用No wrap-in
选项修复此问题。在JSFIDLE中,您选择了onLoad
选项。
如果您从主体中删除内联onload并只放置startTime()代码>在JavaScript的底部,它可以工作。
但这只是JSFIDLE上的情况。在“真实世界”的情况下,您需要将脚本放在页面加载上的事件侦听器的主体或头部
jQuery的缩写是$(function(){/*在页面加载后执行*/})代码>
正如您所提到的,您正在使用jQuery:
在JSFIDLE中,您选择了onLoad
选项。
如果您从主体中删除内联onload并只放置startTime()代码>在JavaScript的底部,它可以工作。
但这只是JSFIDLE上的情况。在“真实世界”的情况下,您需要将脚本放在页面加载上的事件侦听器的主体或头部
jQuery的缩写是$(function(){/*在页面加载后执行*/})代码>
正如您所提到的,您正在使用jQuery:
不久前我用打字机做了一个钟
在这里,我传递时钟的功能
/**
* @class
* @name Time
*/
class Time
{
private millisecond: number;
private second: number;
private minute: number;
private hour: number;
private day: number;
private year: number;
/**
* @hideconstructor
* @param {number} millisecond
* @param {number} second
* @param {number} minute
* @param {number} hour
* @param {number} day
* @param {number} year
* @returns {void}
*/
public constructor(millisecond: number = NaN,
second: number = NaN,
minute: number = NaN,
hour: number = NaN,
day: number = NaN,
year: number = NaN)
{
this.millisecond = millisecond;
this.second = second;
this.minute = minute;
this.hour = hour;
this.day = day;
this.year = year;
}
//Getters and Setters.
/**
* @access public
* @method
* @alias Time.getMillisecond
* @returns {number}
*/
public getMillisecond(): number
{
return this.millisecond;
}
/**
* @access public
* @method
* @alias Time.setMillisecond
* @param {number} millisecond
* @returns {void}
*/
public setMillisecond(millisecond: number): void
{
this.millisecond = millisecond;
}
/**
* @access public
* @method
* @alias Time.getSecond
* @returns {number}
*/
public getSecond(): number
{
return this.second;
}
/**
* @access public
* @method
* @alias Time.setSecond
* @param {number} second
* @returns {void}
*/
public setSecond(second: number): void
{
this.second = second;
}
/**
* @access public
* @method
* @alias Time.getMinute
* @returns {number}
*/
public getMinute(): number
{
return this.minute;
}
/**
* @access public
* @method
* @alias Time.setMinute
* @param {number} minute
* @returns {void}
*/
public setMinute(minute: number): void
{
this.minute = minute;
}
/**
* @access public
* @method
* @alias Time.getHour
* @returns {number}
*/
public getHour(): number
{
return this.hour;
}
/**
* @access public
* @method
* @alias Time.setHour
* @param {number} hour
* @returns {void}
*/
public setHour(hour: number): void
{
this.hour = hour;
}
/**
* @access public
* @method
* @alias Time.getDay
* @returns {number}
*/
public getDay(): number
{
return this.day;
}
/**
* @access public
* @method
* @alias Time.setDay
* @param {number} day
* @returns {void}
*/
public setDay(day: number): void
{
this.day = day;
}
/**
* @access public
* @method
* @alias Time.getYear
* @returns {number}
*/
public getYear(): number
{
return this.year;
}
/**
* @access public
* @method
* @alias Time.setYear
* @param {number} year
* @returns {void}
*/
public setYear(year: number): void
{
this.year = year;
}
/**
* @access public
* @method
* @alias Time.today
* @returns {Date}
*/
public today(): Date
{
return new Date();
}
//Time format.
/**
* @access public
* @method
* @alias Time.year_format
* @returns {string}
*/
public year_format(): string
{
this.setYear(this.today().getFullYear())
if (this.getYear().valueOf() >= 0 &&
this.getYear().valueOf() < 10)
{
return `000${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 10 &&
this.getYear().valueOf() < 100)
{
return `00${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 100 &&
this.getYear().valueOf() < 1000)
{
return `0${this.getYear()}`;
}
else
{
return this.getYear().toString();
}
}
/**
* @access public
* @method
* @alias Time.day_format
* @returns {string}
*/
public day_format(): string
{
this.setDay(this.today().getDate());
if (this.getDay().valueOf() >= 0 &&
this.getDay().valueOf() < 10)
{
return `0${this.getDay()}`;
}
else
{
return this.getDay().toString();
}
}
/**
* @access public
* @method
* @alias Time.hour_format
* @returns {string}
*/
public hour_format(): string
{
this.setHour(this.today().getHours());
if (this.getHour().valueOf() >= 0 &&
this.getHour().valueOf() < 10)
{
return `0${this.getHour()}`;
}
else
{
return this.getHour().toString();
}
}
/**
* @access public
* @method
* @alias Time.minutes_format
* @returns {string}
*/
public minutes_format(): string
{
this.setMinute(this.today().getMinutes());
if (this.getMinute().valueOf() >= 0 &&
this.getMinute().valueOf() < 10)
{
return `0${this.getMinute()}`;
}
else
{
return this.getMinute().toString();
}
}
/**
* @access public
* @method
* @alias Time.second_format
* @returns {string}
*/
public second_format(): string
{
this.setSecond(this.today().getSeconds());
if (this.getSecond().valueOf() >= 0 &&
this.getSecond().valueOf() < 10)
{
return `0${this.getSecond()}`;
}
else
{
return this.getSecond().toString();
}
}
/**
* @access public
* @method
* @alias Time.millisecond_format
* @returns {string}
*/
public millisecond_format(): string
{
this.setMillisecond(this.today().getMilliseconds());
if (this.getMillisecond().valueOf() >= 0 &&
this.getMillisecond().valueOf() < 10)
{
return `000${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 10 &&
this.getMillisecond().valueOf() < 100)
{
return `00${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 100 &&
this.getMillisecond().valueOf() < 1000)
{
return `0${this.getMillisecond()}`;
}
else
{
return this.getMillisecond().toString();
}
}
//Current.
/**
* @access public
* @method
* @alias Time.current_month
* @returns {string}
*/
public current_month(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias months
*/
const months: Array<string> = [
`January`,
`February`,
`March`,
`April`,
`May`,
`June`,
`July`,
`August`,
`September`,
`October`,
`November`,
`December`
];
/**
* @access private
* @constant
* @type {number}
* @alias month
*/
const month: number = this.today().getMonth();
return months[month];
}
/**
* @access public
* @method
* @alias Time.current_day
* @returns {string}
*/
public current_day(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias week
*/
const week: Array<string> = [
`Sunday`,
`Monday`,
`Tuesday`,
`Wednesday`,
`Thursday`,
`Friday`,
`Saturday`
];
/**
* @access private
* @constant
* @type {number}
* @alias weekday
*/
const weekday: number = this.today().getDay();
return week[weekday];
}
}
至少对我来说,这段代码(已经编写了JavaScript并从头开始编写)适合我。我通过从body标记末尾的script标记调用函数来实现它。不久前,我用TypeScript制作了一个时钟
在这里,我传递时钟的功能
/**
* @class
* @name Time
*/
class Time
{
private millisecond: number;
private second: number;
private minute: number;
private hour: number;
private day: number;
private year: number;
/**
* @hideconstructor
* @param {number} millisecond
* @param {number} second
* @param {number} minute
* @param {number} hour
* @param {number} day
* @param {number} year
* @returns {void}
*/
public constructor(millisecond: number = NaN,
second: number = NaN,
minute: number = NaN,
hour: number = NaN,
day: number = NaN,
year: number = NaN)
{
this.millisecond = millisecond;
this.second = second;
this.minute = minute;
this.hour = hour;
this.day = day;
this.year = year;
}
//Getters and Setters.
/**
* @access public
* @method
* @alias Time.getMillisecond
* @returns {number}
*/
public getMillisecond(): number
{
return this.millisecond;
}
/**
* @access public
* @method
* @alias Time.setMillisecond
* @param {number} millisecond
* @returns {void}
*/
public setMillisecond(millisecond: number): void
{
this.millisecond = millisecond;
}
/**
* @access public
* @method
* @alias Time.getSecond
* @returns {number}
*/
public getSecond(): number
{
return this.second;
}
/**
* @access public
* @method
* @alias Time.setSecond
* @param {number} second
* @returns {void}
*/
public setSecond(second: number): void
{
this.second = second;
}
/**
* @access public
* @method
* @alias Time.getMinute
* @returns {number}
*/
public getMinute(): number
{
return this.minute;
}
/**
* @access public
* @method
* @alias Time.setMinute
* @param {number} minute
* @returns {void}
*/
public setMinute(minute: number): void
{
this.minute = minute;
}
/**
* @access public
* @method
* @alias Time.getHour
* @returns {number}
*/
public getHour(): number
{
return this.hour;
}
/**
* @access public
* @method
* @alias Time.setHour
* @param {number} hour
* @returns {void}
*/
public setHour(hour: number): void
{
this.hour = hour;
}
/**
* @access public
* @method
* @alias Time.getDay
* @returns {number}
*/
public getDay(): number
{
return this.day;
}
/**
* @access public
* @method
* @alias Time.setDay
* @param {number} day
* @returns {void}
*/
public setDay(day: number): void
{
this.day = day;
}
/**
* @access public
* @method
* @alias Time.getYear
* @returns {number}
*/
public getYear(): number
{
return this.year;
}
/**
* @access public
* @method
* @alias Time.setYear
* @param {number} year
* @returns {void}
*/
public setYear(year: number): void
{
this.year = year;
}
/**
* @access public
* @method
* @alias Time.today
* @returns {Date}
*/
public today(): Date
{
return new Date();
}
//Time format.
/**
* @access public
* @method
* @alias Time.year_format
* @returns {string}
*/
public year_format(): string
{
this.setYear(this.today().getFullYear())
if (this.getYear().valueOf() >= 0 &&
this.getYear().valueOf() < 10)
{
return `000${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 10 &&
this.getYear().valueOf() < 100)
{
return `00${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 100 &&
this.getYear().valueOf() < 1000)
{
return `0${this.getYear()}`;
}
else
{
return this.getYear().toString();
}
}
/**
* @access public
* @method
* @alias Time.day_format
* @returns {string}
*/
public day_format(): string
{
this.setDay(this.today().getDate());
if (this.getDay().valueOf() >= 0 &&
this.getDay().valueOf() < 10)
{
return `0${this.getDay()}`;
}
else
{
return this.getDay().toString();
}
}
/**
* @access public
* @method
* @alias Time.hour_format
* @returns {string}
*/
public hour_format(): string
{
this.setHour(this.today().getHours());
if (this.getHour().valueOf() >= 0 &&
this.getHour().valueOf() < 10)
{
return `0${this.getHour()}`;
}
else
{
return this.getHour().toString();
}
}
/**
* @access public
* @method
* @alias Time.minutes_format
* @returns {string}
*/
public minutes_format(): string
{
this.setMinute(this.today().getMinutes());
if (this.getMinute().valueOf() >= 0 &&
this.getMinute().valueOf() < 10)
{
return `0${this.getMinute()}`;
}
else
{
return this.getMinute().toString();
}
}
/**
* @access public
* @method
* @alias Time.second_format
* @returns {string}
*/
public second_format(): string
{
this.setSecond(this.today().getSeconds());
if (this.getSecond().valueOf() >= 0 &&
this.getSecond().valueOf() < 10)
{
return `0${this.getSecond()}`;
}
else
{
return this.getSecond().toString();
}
}
/**
* @access public
* @method
* @alias Time.millisecond_format
* @returns {string}
*/
public millisecond_format(): string
{
this.setMillisecond(this.today().getMilliseconds());
if (this.getMillisecond().valueOf() >= 0 &&
this.getMillisecond().valueOf() < 10)
{
return `000${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 10 &&
this.getMillisecond().valueOf() < 100)
{
return `00${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 100 &&
this.getMillisecond().valueOf() < 1000)
{
return `0${this.getMillisecond()}`;
}
else
{
return this.getMillisecond().toString();
}
}
//Current.
/**
* @access public
* @method
* @alias Time.current_month
* @returns {string}
*/
public current_month(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias months
*/
const months: Array<string> = [
`January`,
`February`,
`March`,
`April`,
`May`,
`June`,
`July`,
`August`,
`September`,
`October`,
`November`,
`December`
];
/**
* @access private
* @constant
* @type {number}
* @alias month
*/
const month: number = this.today().getMonth();
return months[month];
}
/**
* @access public
* @method
* @alias Time.current_day
* @returns {string}
*/
public current_day(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias week
*/
const week: Array<string> = [
`Sunday`,
`Monday`,
`Tuesday`,
`Wednesday`,
`Thursday`,
`Friday`,
`Saturday`
];
/**
* @access private
* @constant
* @type {number}
* @alias weekday
*/
const weekday: number = this.today().getDay();
return week[weekday];
}
}
至少对我来说,这段代码(已经编写了JavaScript并从头开始编写)适合我。我通过从body标记末尾的script标记调用函数来实现它。如何加载javascript文件?我编辑了我的原始帖子。谢谢你的回复!我把它放在html文档的开头,在JSFIDLE中,您选择了onLoad
选项。因此,如果您从主体中删除内联onload,只需放入startTime()代码>在JavaScript的底部,它可以工作。下面是重新创建您的情况的方法。(您还可以将函数移到$(function(){…})
上方)嘿,nvm真管用!谢谢:-)如果您想将此作为答复提交,我将为您接受!你是如何加载你的javascript文件的?我编辑了我的原始帖子。谢谢你的回复!我把它放在html文档的开头,在JSFIDLE中,您选择了onLoad
选项。因此,如果您从主体中删除内联onload,只需放入startTime()代码>在JavaScript的底部,它可以工作。下面是重新创建您的情况的方法。(您还可以将函数移到$(function(){…})
上方)嘿,nvm真管用!谢谢:-)如果您想将此作为答复提交,我将为您接受!谢谢你的回复!实际上,我已经在$(文档)中有了它。我的js文件中的ready函数,但它只是使它成为一个静态时钟,在我刷新之前不会移动:-(它在小提琴中工作,您确定您正在以相同的方式使用它吗?(正文中没有onload属性?)谢谢您的响应!我实际上已经在$(文档)中有了它.ready函数在我的js文件中,但它只是使它成为一个静态时钟,在我刷新之前不会移动:-(它在小提琴中工作,您确定您以相同的方式使用它吗?(正文中没有onload属性?)
$(document).ready(function (){
startTime();
});
function startTime()
{
/* ... */
}
// after the whole page is loaded, execute the previously declared startTime()
$(function() {
startTime();
});
/**
* @class
* @name Time
*/
class Time
{
private millisecond: number;
private second: number;
private minute: number;
private hour: number;
private day: number;
private year: number;
/**
* @hideconstructor
* @param {number} millisecond
* @param {number} second
* @param {number} minute
* @param {number} hour
* @param {number} day
* @param {number} year
* @returns {void}
*/
public constructor(millisecond: number = NaN,
second: number = NaN,
minute: number = NaN,
hour: number = NaN,
day: number = NaN,
year: number = NaN)
{
this.millisecond = millisecond;
this.second = second;
this.minute = minute;
this.hour = hour;
this.day = day;
this.year = year;
}
//Getters and Setters.
/**
* @access public
* @method
* @alias Time.getMillisecond
* @returns {number}
*/
public getMillisecond(): number
{
return this.millisecond;
}
/**
* @access public
* @method
* @alias Time.setMillisecond
* @param {number} millisecond
* @returns {void}
*/
public setMillisecond(millisecond: number): void
{
this.millisecond = millisecond;
}
/**
* @access public
* @method
* @alias Time.getSecond
* @returns {number}
*/
public getSecond(): number
{
return this.second;
}
/**
* @access public
* @method
* @alias Time.setSecond
* @param {number} second
* @returns {void}
*/
public setSecond(second: number): void
{
this.second = second;
}
/**
* @access public
* @method
* @alias Time.getMinute
* @returns {number}
*/
public getMinute(): number
{
return this.minute;
}
/**
* @access public
* @method
* @alias Time.setMinute
* @param {number} minute
* @returns {void}
*/
public setMinute(minute: number): void
{
this.minute = minute;
}
/**
* @access public
* @method
* @alias Time.getHour
* @returns {number}
*/
public getHour(): number
{
return this.hour;
}
/**
* @access public
* @method
* @alias Time.setHour
* @param {number} hour
* @returns {void}
*/
public setHour(hour: number): void
{
this.hour = hour;
}
/**
* @access public
* @method
* @alias Time.getDay
* @returns {number}
*/
public getDay(): number
{
return this.day;
}
/**
* @access public
* @method
* @alias Time.setDay
* @param {number} day
* @returns {void}
*/
public setDay(day: number): void
{
this.day = day;
}
/**
* @access public
* @method
* @alias Time.getYear
* @returns {number}
*/
public getYear(): number
{
return this.year;
}
/**
* @access public
* @method
* @alias Time.setYear
* @param {number} year
* @returns {void}
*/
public setYear(year: number): void
{
this.year = year;
}
/**
* @access public
* @method
* @alias Time.today
* @returns {Date}
*/
public today(): Date
{
return new Date();
}
//Time format.
/**
* @access public
* @method
* @alias Time.year_format
* @returns {string}
*/
public year_format(): string
{
this.setYear(this.today().getFullYear())
if (this.getYear().valueOf() >= 0 &&
this.getYear().valueOf() < 10)
{
return `000${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 10 &&
this.getYear().valueOf() < 100)
{
return `00${this.getYear()}`;
}
else
if (this.getYear().valueOf() >= 100 &&
this.getYear().valueOf() < 1000)
{
return `0${this.getYear()}`;
}
else
{
return this.getYear().toString();
}
}
/**
* @access public
* @method
* @alias Time.day_format
* @returns {string}
*/
public day_format(): string
{
this.setDay(this.today().getDate());
if (this.getDay().valueOf() >= 0 &&
this.getDay().valueOf() < 10)
{
return `0${this.getDay()}`;
}
else
{
return this.getDay().toString();
}
}
/**
* @access public
* @method
* @alias Time.hour_format
* @returns {string}
*/
public hour_format(): string
{
this.setHour(this.today().getHours());
if (this.getHour().valueOf() >= 0 &&
this.getHour().valueOf() < 10)
{
return `0${this.getHour()}`;
}
else
{
return this.getHour().toString();
}
}
/**
* @access public
* @method
* @alias Time.minutes_format
* @returns {string}
*/
public minutes_format(): string
{
this.setMinute(this.today().getMinutes());
if (this.getMinute().valueOf() >= 0 &&
this.getMinute().valueOf() < 10)
{
return `0${this.getMinute()}`;
}
else
{
return this.getMinute().toString();
}
}
/**
* @access public
* @method
* @alias Time.second_format
* @returns {string}
*/
public second_format(): string
{
this.setSecond(this.today().getSeconds());
if (this.getSecond().valueOf() >= 0 &&
this.getSecond().valueOf() < 10)
{
return `0${this.getSecond()}`;
}
else
{
return this.getSecond().toString();
}
}
/**
* @access public
* @method
* @alias Time.millisecond_format
* @returns {string}
*/
public millisecond_format(): string
{
this.setMillisecond(this.today().getMilliseconds());
if (this.getMillisecond().valueOf() >= 0 &&
this.getMillisecond().valueOf() < 10)
{
return `000${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 10 &&
this.getMillisecond().valueOf() < 100)
{
return `00${this.getMillisecond()}`;
}
else
if (this.getMillisecond().valueOf() >= 100 &&
this.getMillisecond().valueOf() < 1000)
{
return `0${this.getMillisecond()}`;
}
else
{
return this.getMillisecond().toString();
}
}
//Current.
/**
* @access public
* @method
* @alias Time.current_month
* @returns {string}
*/
public current_month(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias months
*/
const months: Array<string> = [
`January`,
`February`,
`March`,
`April`,
`May`,
`June`,
`July`,
`August`,
`September`,
`October`,
`November`,
`December`
];
/**
* @access private
* @constant
* @type {number}
* @alias month
*/
const month: number = this.today().getMonth();
return months[month];
}
/**
* @access public
* @method
* @alias Time.current_day
* @returns {string}
*/
public current_day(): string
{
//Constants.
/**
* @access private
* @constant
* @type {Array<string>}
* @alias week
*/
const week: Array<string> = [
`Sunday`,
`Monday`,
`Tuesday`,
`Wednesday`,
`Thursday`,
`Friday`,
`Saturday`
];
/**
* @access private
* @constant
* @type {number}
* @alias weekday
*/
const weekday: number = this.today().getDay();
return week[weekday];
}
}
/**
* @class
* @name Clock
*/
class Clock extends Time
{
/**
* @hideconstructor
* @param {number} millisecond
* @param {number} second
* @param {number} minute
* @param {number} hour
* @param {number} day
* @param {number} year
* @returns {void}
*/
public constructor(millisecond: number = NaN,
second: number = NaN,
minute: number = NaN,
hour: number = NaN,
day: number = NaN,
year: number = NaN)
{
super
(
millisecond,
second,
minute,
hour,
day,
year
);
}
/**
* @access private
* @method
* @alias Clock.span_day
* @param {string} id
* @returns {HTMLSpanElement}
*/
private span_day(id: string): HTMLSpanElement
{
return (window.document.getElementById(id) as HTMLSpanElement);
}
/**
* @access private
* @method
* @alias Clock.span_time
* @param {string} id
* @returns {HTMLSpanElement}
*/
private span_time(id: string): HTMLSpanElement
{
return (window.document.getElementById(id) as HTMLSpanElement);
}
/**
* @access private
* @method
* @alias Clock.time
* @returns {string}
*/
private time(): string
{
//Objects.
/**
* @access private
* @constant
* @type {object}
* @alias time
*/
const time: Time = new Time();
return `${time.hour_format()}:${time.minutes_format()}:${time.second_format()}:${time.millisecond_format()}`;
}
/**
* @access private
* @method
* @alias Clock.short_date
* @returns {string}
*/
private short_date(): string
{
//Objects.
/**
* @access private
* @constant
* @type {object}
* @alias time
*/
const time: Time = new Time();
return time.today().toLocaleDateString();
}
/**
* @access private
* @method
* @alias Clock.long_date
* @returns {string}
*/
private long_date(): string
{
//Objects.
/**
* @access private
* @constant
* @type {object}
* @alias time
*/
const time: Time = new Time();
return `${time.current_day()}, ${time.current_month()} ${time.day_format()}, ${time.year_format()}`;
}
//Clock.
/**
* @access public
* @method
* @alias Clock.clock__short
* @param {string} day_id
* @param {string} time_id
* @returns {void}
*/
public clock__short(day_id: string,
time_id: string): void
{
window.setInterval(function ()
{
/**
* @access private
* @constant
* @type {object}
* @alias clock
*/
const clock: Clock = new Clock();
clock.span_day(day_id).innerHTML = clock.short_date();
}, 0);
window.setInterval(function ()
{
/**
* @access private
* @constant
* @type {object}
* @alias clock
*/
const clock: Clock = new Clock();
clock.span_time(time_id).innerHTML = clock.time();
}, 0);
}
/**
* @access public
* @method
* @alias Clock.clock__long
* @param {string} day_id
* @param {string} time_id
* @returns {void}
*/
public clock__long(day_id: string,
time_id: string): void
{
window.setInterval(function ()
{
/**
* @access private
* @constant
* @type {object}
* @alias clock
*/
const clock: Clock = new Clock();
clock.span_day(day_id).innerHTML = clock.long_date();
}, 0);
window.setInterval(function ()
{
/**
* @access private
* @constant
* @type {object}
* @alias Clock.clock__long.clock
*/
const clock: Clock = new Clock();
clock.span_time(time_id).innerHTML = clock.time();
}, 0);
}
}
/**
* @function
* @name clock
* @param {string} day_id This ID refers to the ID of the HTML where you want to put the day.
* @param {string} time_id This ID refers to the ID of the HTML where you want to put the time.
* @param {string} type The "type" refers to whether the clock is Long or Short.
* @returns {void}
* @summary This function is to insert a clock on the page.
*/
function clock(day_id: string,
time_id: string,
type: string): void
{
try
{
//Objects.
/**
* @access private
* @constant
* @type {object}
* @alias clock
*/
const clock: Clock = new Clock();
if (type === `Long`)
{
clock.clock__long(day_id, time_id);
}
else
if (type === `Short`)
{
clock.clock__short(day_id, time_id);
}
}
catch (exception)
{
window.console.error(`Unexpected error. | Error name: ${(exception as Error).name}; Error message: ${(exception as Error).message}`);
}
}