• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.5 Make javascript only work on specific dates possible?

borgqueenx

Active member
#1
Im having some javascripts on my site (snow effect) and it works/gets called by an edit in a template file.
I want it to work on between 20 december and 20 january. is there a way to do this?
 

Jeremy

Well-known member
#4
Code:
if (Date.now() >= Date(2015, 11, 20) && Date.now() <= Date(2016, 0, 20)) {
  // your javascript here
}
 

Jeremy

Well-known member
#6
No.

But you could generalize it by replacing it with Date.prototype.getFullYear()

Code:
var now = Date.now();
if (now >= Date.parse(Date(now.getFullYear(), 11, 20)) && now <= Date.parse(Date(now.getFullYear() + 1, 0, 20))) {
// code
}
 
Last edited:

Jeremy

Well-known member
#10
I've updated the code, it appears that using Date as a constructor gives a string and not a milliseconds/object. Just checked it out in the console and it works.
 

Jake B.

Well-known member
#11
Really unrelated to this thread, but if you're going to do that snowfalling thing, it may be a good idea to create a custom user field that will let users disable that as it can get fairly annoying if you're trying to read something :)
 

borgqueenx

Active member
#12
this is what i have and not working currently.
<xen:require js="js/xenforo/snowfall.jquery.min.js"/>
<script>
var now = Date.now();
if (now >= Date.parse(Date(now.getFullYear(), 11, 7)) && now <= Date.parse(Date(now.getFullYear() + 1, 0, 20))) {
$(document).snowfall({flakeCount : 95, maxSpeed : 10, maxSize : 5,round : true});
}
</script>
 

borgqueenx

Active member
#13
Really unrelated to this thread, but if you're going to do that snowfalling thing, it may be a good idea to create a custom user field that will let users disable that as it can get fairly annoying if you're trying to read something :)
will be difficult....instead im just having the effect small/slightly so its not that intrusive.
 

Jake B.

Well-known member
#14
will be difficult....instead im just having the effect small/slightly so its not that intrusive.
will be difficult....instead im just having the effect small/slightly so its not that intrusive.
Not difficult at all, create a custom user field with the id "disableSnowfall" and do this:

Code:
<xen:if is="!{$visitor.customFields.disableSnowfall}">
[Your JS here]
</xen:if>
 

borgqueenx

Active member
#15
Not difficult at all, create a custom user field with the id "disableSnowfall" and do this:

Code:
<xen:if is="!{$visitor.customFields.disableSnowfall}">
[Your JS here]
</xen:if>
still doesn't tell me everything. what javascript to put there? if i understand correctly this makes it always work or always disabled. I want it only work during my specific time, and then that the user has the option to disable it.
 

Jeremy

Well-known member
#16
Reasons I hate JavaScript...

Code:
var now = new Date();
if (now >= new Date(now.getFullYear(), 11, 20) && now <= new Date(now.getFullYear() + 1, 0, 20)) {
// code
}
Screen Shot 2015-12-08 at 5.11.24 PM.png
 

James

Well-known member
#17
still doesn't tell me everything. what javascript to put there? if i understand correctly this makes it always work or always disabled. I want it only work during my specific time, and then that the user has the option to disable it.
Jake's point is that you should give the users the ability to disable it. Put the javascript between the conditional statement he provided and people can disable it if they don't want it.
 

borgqueenx

Active member
#18
Jake's point is that you should give the users the ability to disable it. Put the javascript between the conditional statement he provided and people can disable it if they don't want it.
currently the script is in a template logo_block. so adding it to the user's field will probaly not work. i think.