0

我刚刚在服务器上设置了一个简短的玩具学习 ColdFusion 页面。该页面调用 cf 函数,该函数获取当前日期、确定年份,然后返回反映当前年份是否为闰年的布尔值。此信息以纯文本形式显示在主页上。

页面.cfm

<html>
<head>
        <cfinclude template="./func.cfm" />
</head>
<body>
        <cfset yearList = "2000;2001,2002/2003,2004,2005;2006/2007,2008,2009;2010,2011,2012" >

        <cfloop index="year" list=#yearList# delimiters=",;/" >

                <cfset isLeapYear = #My_IsLeapYear(year)# >

                <cfif isLeapYear is True>
                        <cfoutput>
                                #year# is a leap year!
                        </cfoutput>

                        <cfelse>
                                <cfoutput>
                                #year# is not a leap year.
                                </cfoutput>
                </cfif>
                <br>
        </cfloop>

</body>
</html>

函数cfm

<cffunction name="My_IsLeapYear" output="false" access="public" returnType="boolean">
        <cfargument name="year" type="numeric" required="true" default="" />
        <cfset var isLeapYear = (DaysInYear(CreateDate(arguments.year,1,1)) EQ 366) />

        <cfreturn isLeapYear>
</cffunction>

试图访问此页面导致了可怕的内存泄漏并关闭了托管它的服务器。我不知所措。有什么想法吗?

4

2 回答 2

0

内存泄漏是由一个奇怪的外部问题引起的。感谢您的评论等。

于 2015-02-04T21:30:43.800 回答
0

我认为这是一种更快的检查方法:

<cfset yearList = "2000;2001,2002/2003,2004,2005;2006/2007,2008,2009;2010,2011,2012" >

<cfoutput>
<cfloop index="year" list="#yearList#" delimiters=",;/" >
        <p>#year# is <cfif !(val(year) MOD 4 EQ 0)>not </cfif>a leap year!</p>
</cfloop>
</cfoutput>

避免更昂贵的调用DaysInYear(). 您只需要检查 的值year是否是可被 4 整除的数字。

更新

点re:计算。至于原始函数,您可以只返回比较的结果。无需创建函数局部变量。

<cffunction name="My_IsLeapYear" output="false" access="public" returnType="boolean">
        <cfargument name="year" type="numeric" required="true" default="" />
        <cfreturn (DaysInYear(CreateDate(arguments.year,1,1)) EQ 366) />
</cffunction>

在 page.cfm 中,您可以更改此代码:

<cfset isLeapYear = #My_IsLeapYear(year)# >
<cfif isLeapYear is True>

更简单的调用:

<cfif My_IsLeapYear(year)>

因为该函数只会返回一个布尔值。

于 2015-02-04T20:53:43.950 回答