Javascript匿名函数(Javascript anonymous functions)
我有以下代码:
for (var i=0; i < 20; i++) { var itemButton = $('<button />').click(function(){ alert('Hello'); }).append('Hello'); $('#container').append(itemButton); }
我创建了许多按钮,每个按钮都有一个onclick功能。 然后我使用
$('#shopSearchTopBar').empty();
清除$('#shopSearchTopBar').empty();
并再次创建按钮。我的问题是:这可能是内存泄漏吗? 当我在#container上调用.empty()方法或内存被释放时,Javascript是否释放了我第一次创建的匿名函数所使用的内存,我可以安全地添加这20个新按钮,每个按钮都有自己的新匿名函数?
I have the following code:
for (var i=0; i < 20; i++) { var itemButton = $('<button />').click(function(){ alert('Hello'); }).append('Hello'); $('#container').append(itemButton); }
Where i create lots of buttons each with a onclick function. Then i clear the #container using
$('#shopSearchTopBar').empty();
and create the buttons again.My question is: Is this a possible memory leak? Does Javascript frees the memory used for those anonymous functions i created the first time when i call the .empty() method on the #container or the memory gets freed an i can safely add those 20 new buttons each with his own new anonymous function?
原文:https://stackoverflow.com/questions/4879686
最满意答案
看来这应该做你需要的。
if ([dateToCompare laterDate:pastSevenDays] == dateToCompare) { // dateToCompare is less than 7 days ago } else if ([dateToCompare laterDate:pastFourteenDays] == dateToCompare) { // dateToCompare is more than 7 days ago, but less than 14 days ago } else if ([dateToCompare laterDate:pastThirtyDays] == dateToCompare) { // dateToCompare is more than 14 days ago, but less than 30 days ago }
It seems like this should do what you need.
if ([dateToCompare laterDate:pastSevenDays] == dateToCompare) { // dateToCompare is less than 7 days ago } else if ([dateToCompare laterDate:pastFourteenDays] == dateToCompare) { // dateToCompare is more than 7 days ago, but less than 14 days ago } else if ([dateToCompare laterDate:pastThirtyDays] == dateToCompare) { // dateToCompare is more than 14 days ago, but less than 30 days ago }
相关问答
更多-
如何确定Java中两个日期之间的日期?(How can I determine if a date is between two dates in Java? [duplicate])[2023-06-07]
如果您不知道最小/最大值的顺序 Date a, b; // assume these are set to something Date d; // the date in question return a.compareTo(d) * d.compareTo(b) > 0; 如果你希望范围是包容性的 return a.compareTo(d) * d.compareTo(b) >= 0; If you don't know the order of the min/max value ... -
我想出了这个: from datetime import date, timedelta d1 = date(2008, 8, 15) # start date d2 = date(2008, 9, 15) # end date delta = d2 - d1 # timedelta for i in range(delta.days + 1): print(d1 + timedelta(days=i)) 输出: 2008-08-15 2008-08-16 ... 200 ...
-
如何检查日期范围的日期位于mysql查询中的两个日期之间(How to check a dates of a date range lies in between two dates in mysql query)[2024-01-02]
date_from < '2011-10-20' AND date_to > '2011-10-14' date_from < '2011-10-20' AND date_to > '2011-10-14' -
qv = AthleteSubscription.objects.filter(subscription_start__gte=some_date, subscription_end__lte=some_date) qv = AthleteSubscription.objects.filter(subscription_start__gte=some_date, subscription_end__lte=some_date)
-
它看起来像你可以用简单的while循环实现你想要的 - 只需要将日期逐个添加到初始日期,直到你达到日期范围的末尾。 所以它看起来像 val interimDates = arrayListOf
() var initial: Date = Date()// obtained earlier val c = Calendar.getInstance() c.time = initial while (initial < finalDate /*also obtained earlier*/) { ... -
看来这应该做你需要的。 if ([dateToCompare laterDate:pastSevenDays] == dateToCompare) { // dateToCompare is less than 7 days ago } else if ([dateToCompare laterDate:pastFourteenDays] == dateToCompare) { // dateToCompare is more than 7 days ago, but less than 1 ...
-
你使用错误的格式月份。 MM用于月份, mm用于分钟 [df setDateFormat:@"mm/dd/yyyy hh:mm:ss a"]; 应该 [df setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]; 你得到的01是从约会开始的分钟数。 You are using wrong format for month. MM is used for month and mm is used for minutes [df setDateFormat:@"mm/dd/yyyy ...
-
显示日期选择器,日期位于jquery中两个给定日期之间(Display a datepicker with dates lies between two given dates in jquery)[2022-09-05]
这是如何实现两个日期之间的日期。 希望这可以帮助你,正如本页的@cfs所说,你可以通过这个脚本禁用前几天: $(function() { $( "#datepicker,#datepicker1" ).datepicker({ minDate:new Date() }); }); var start = $("#from").datepicker("getDate"), end = $("#to").datepicker("getDate"), curren ... -
如果上面使用的API类似于原生的Hibernate Restrictions API(我猜它是,因为它更合乎逻辑),你只需使用ge ,你应该使用le ,反之亦然: le("startDate", coTimeStamp) // startDate <= current date ge("endDate", coTimeStamp) // endDate >= current date If the API used above is similar to the native Hibernate Rest ...