missing / RegEx code [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I trying to append the script to the page but I get the answer:
Invalid regular expression: missing /
Can someone please take a look and show me where is the problem in my RegEx code?
function init(){
var script = document.createElement("script");
script.type = "text/javascript";
script.text = 'jx = {'+
'b: function () {'+
'var b = !1;'+
'if ("undefined" != typeof ActiveXObject) { try {'+
'b = new ActiveXObject("Msxml2.XMLHTTP");'+
' } catch (c) {'+
' try {'+
' b = new ActiveXObject("Microsoft.XMLHTTP");'+
' } catch (a) {'+
' b = !1;'+
' }'+
' } } else { if (window.XMLHttpRequest) try {'+
' b = new XMLHttpRequest;'+
'} catch (h) {'+
' b = !1;'+
' }'+
'}'+
' return b;'+
' },'+
'load: function (b, c, a, h, g) {'+
' var e = this.d();'+
' if (e && b) {'+
' e.overrideMimeType && e.overrideMimeType("text/xml");'+
' h || (h = "GET");'+
' a || (a = "text");'+
' g || (g = {});'+
' a = a.toLowerCase();'+
' h = h.toUpperCase();'+
' b += b.indexOf("?") + 1 ? "&" : "?";'+
' var k = null;'+
' "POST" == h && (k = b.split("?"), b = k[0], k = k[1]);'+
' e.open(h, b, !0);'+
' e.onreadystatechange = g.c ? function () {'+
' g.c(e)'+
'} : function () {'+
' if (4 == e.readyState)'+
' if (200 == e.status) {'+
' var b = "";'+
' e.responseText && (b = e.responseText);'+
' "j" == a.charAt(0) ? (b = b.replace(/[\n\r]/g, ""), b = eval("(" + b + ")")) : "x" == a.charAt(0) && (b = e.responseXML);'+
' c && c(b)'+
' } else g.f && document.getElementsByTagName("body")[0].removeChild(g.f), g.e && (document.getElementById(g.e).style.display = "none"), error && error(e.status)'+
' };'+
' e.send(k);'+
' }'+
'},'+
' d: function () {'+
' return this.b()'+
' }'+
'};'+
'alert("loaded");';
document.body.appendChild(script);
}
Do you see a mistake!

quite apart from what you're doing, the problem is that you're not escaping the backslashes. the reges
/[\n\r]/g
By itself is perfectly fine, but you're not writing a literal RegExp instance, you're writing a string. In a string \n is an escape sequence for a new line. To get around this, escape the backslash:
var str = '/[\\n\\r]/g';
would do the trick

Related

How to prevent decode encodeURIComponent in address bar in mobile device? Google Blogger Error - Pagination is not working correct in mobile device

Please read carefully. The problem is coming in the Google Blogger template. Pagination are not working properly in the mobile devices. It is working fine in the Desktop and tablet mode. The pagination is the code of JavaScript.
I tried to find the issue and I got that - "URL is not encoding in the address bar in the mobile mode". That's why the pagination buttons are not showing as per the number of pages. Due to this blogger unable to read the proper url in address bar. See below example
Correct Encoded Address in Desktop- https://www.example.com/search?updated-max=2019-06-25T21%3A53%3A00%2B05%3A30&max-results=4
Incorrect Address in Mobile - https://www.example.com/search?updated-max=2019-06-25T21:53:00%2B05:30&max-results=4&m=1
2019-06-25T21:53:00+05:30 This part of URL in Address Bar should be encoded.
Means 2019-06-25T21:53:00+05:30 should be 2019-06-25T21%3A53%3A00%2B05%3A30 in Mobile same as Desktop. But the colon(:) is not encoding in %3A. This is the main issue.
If I manually edit the URL in address bar and write %3A instead of colon(:) and press the enter, then it is working correct.
I debug the JavaScript Code from the chrom developer tool. I applied the breakpoint here f = encodeURIComponent(f); Then I got that - the encoding is working fine in Desktop and Mobile too. This is shocking.
So, what's the problem? Why the URL is not encoding in the address bar in Mobile mode? 90% I guess that - the Encoded URL part is again Decoding in mobile mode. Means colon(:) is encoding in %3A then again decoding in colon(:) in mobile mode.
This is the problem analysis by me. But I may be wrong. I haven't much stronger knowledge of JS. This kind of issue is shocking and facing first time.
<script>
/*JavaScript Code of Pagination*/
var pageNaviConf = {
perPage: 4,
numPages: 4,
firstText: "First ",
lastText: "Last ",
nextText: "Next ",
prevText: "Prev "
}
//<![CDATA[
function pageNavi(o) {
var m = location.href,
l = m.indexOf("/search/label/") != -1,
a = l ? m.substr(m.indexOf("/search/label/") + 14, m.length) : "";
a = a.indexOf("?") != -1 ? a.substr(0, a.indexOf("?")) : a;
var g = l ? "/search/label/" + a + "?updated-max=" : "/search?updated-max=",
k = o.feed.entry.length,
e = Math.ceil(k / pageNaviConf.perPage);
if (e <= 1) {
return
}
var n = 1,
h = [""];
l ? h.push("/search/label/" + a + "?max-results=" + pageNaviConf.perPage) : h.push("/?max-results=" + pageNaviConf.perPage);
for (var d = 2; d <= e; d++) {
var c = (d - 1) * pageNaviConf.perPage - 1,
b = o.feed.entry[c].published.$t,
f = b.substring(0, 19) + b.substring(23, 29);
f = encodeURIComponent(f);
if (m.indexOf(f) != -1) {
n = d
}
h.push(g + f + "&max-results=" + pageNaviConf.perPage)
}
pageNavi.show(h, n, e)
}
pageNavi.show = function(f, e, a) {
var d = Math.floor((pageNaviConf.numPages - 1) / 2),
g = pageNaviConf.numPages - 1 - d,
c = e - d;
if (c <= 0) {
c = 1
}
endPage = e + g;
if ((endPage - c) < pageNaviConf.numPages) {
endPage = c + pageNaviConf.numPages - 1
}
if (endPage > a) {
endPage = a;
c = a - pageNaviConf.numPages + 1
}
if (c <= 0) {
c = 1
}
var b = '<span class="pages">Pages ' + e + ' of ' + a + "
</span> ";
if(c > 1){
b += '<a href="' + f[1] + '">' + pageNaviConf.firstText
+ "</a>"
}
if (e > 1) {
b += '<a href="' + f[e - 1] + '">' +
pageNaviConf.prevText + "</a>"
}
if (e < a) {
b += '<a href="' + f[e + 1] + '">' +
pageNaviConf.nextText + "</a>"
}
if (endPage < a) {
b += '<a href="' + f[a] + '">' + pageNaviConf.lastText +
" </a>"
}
document.write(b)
};
(function() {
var b = location.href;
if (b.indexOf("?q=") != -1 || b.indexOf(".html") != -1) {
return
}
var d = b.indexOf("/search/label/") + 14;
if (d != 13) {
var c = b.indexOf("?"),
a = (c == -1) ? b.substring(d) : b.substring(d, c);
document.write('<script type="text/javascript"
src="/feeds/posts/summary/-/' + a + '?alt=json-in-script&callback=pageNavi&max-results=99999"><\/script>')
}
else {
document.write('<script type="text/javascript" src="/feeds/posts/summary?alt=json-in-script&callback=pageNavi&max-results=99999"><\/script>')
}
})()
//]]>
</script>
Overall our target is to Encode colon(:) into %3A in address bar in Mobile. I couldn't find any strong solution on the internet. I humble request to please look for it briefly and fix this issue.
Thanks in advance.

Matching a String with Array of String in Java Script

str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
if (str1 = array B){
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'B000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array C) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'C000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array E) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'E000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array F) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'F000'
print ('Comm_Code = ' + comm_code + '\n')
}
else {
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n')
comm_code = 'D000'
print ('Comm_Code = ' + comm_code + '\n')
}
Hello,
I want to know how to match the string Str1 with the value of the Array B,C,E,F.
I mean :
If Str1 = 800|| 807 || 826 || 847 || 866, Then Comm_code = B000
If Str1 = 827 || 846 then Comm_code = C000
If Str1 = 867 || 879 then Comm_code = E000
If Str1 = 880 || 899 then Comm_code = F000
Else Default --> Comm_code = D000
Please kindly advice.
p.s. : Fyi, I'm using EcmaScript 2015 / ES5.
Just use a simple String.prototype.indexOf:
str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
if (B.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'B000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (C.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'C000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (E.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'E000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (F.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'F000';
print ('Comm_Code = ' + comm_code + '\n');
}
else
{
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
comm_code = 'D000';
print ('Comm_Code = ' + comm_code + '\n');
}
You can achieve this with simple if else conditions using Array.indexOf() Methods. However make sure the str1 and the values in the array are of same variable type(string or a number).
if (B.indexOf(str1) > -1 ) { //if value exists in B
//do soemthing;
}
else if(C.indexof(str1) >-1 ) { //if value exists in C
//do soemthing
}
One possible solution to solve this is use Array.some() (I think it is included on ES5 based on this link). First you can create a method that will check if an element is on an array:
function arrayIncludes(arr, ele)
{
return arr.some(function(x) {return (x === ele);});
}
console.log(arrayIncludes([1,2], 2));
console.log(arrayIncludes([1,2], 5));
.as-console {background-color:black !important; color:lime;}
Then, your code can be reworked to this:
str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
function arrayIncludes(arr, ele)
{
return arr.some(function(x) {return (x === ele);});
}
if (arrayIncludes(B, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'B000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(C, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'C000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(E, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'E000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(F, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'F000';
print ('Comm_Code = ' + comm_code + '\n');
}
else
{
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
comm_code = 'D000';
print ('Comm_Code = ' + comm_code + '\n');
}

How can I add regex for this one?

I want to add comma between Haircut and Wash. basically like this: Haircut, Wash And Blow Dry
if(string === 'HaircutWashAndBlowDry'){
string.charAt(0).toUpperCase() + string.slice(1);
str = str.replace(/([A-Z])/g, ' $1').trim();
}
You don't need to lowercase first letter. Use \B along with a simple counter:
var str = 'HaircutWashAndBlowDry';
var i = 1;
console.log(str.replace(/\B([A-Z])/g, function(match, $1) {
return ( i++ == 1 ? ', ' : ' ' ) + $1;
}))
Funny goal :) Let's play a bit with two previous answers (testCase 2 & testCase 3) that relies on words positions and a wider approach that relies on splitting on the And string to behave differently :
// Haircut, Wash And Blow Dry
let strings = [
'HaircutWashAndBlowDry',
'HaircutWashCleanAndBlowDrySet'
];
function testCase1(str) {
let pieces = str.split('And');
pieces[0] = pieces[0].replace(/([a-z])([A-Z])/g, '$1, $2');
pieces[1] = pieces[1].replace(/([a-z])([A-Z])/g, '$1 $2');
return pieces[0] + ' And ' + pieces[1];
}
function testCase2(string) {
return string.replace(/^([A-Z][^A-Z]*)([A-Z])|([A-Z])/g, function($0,$1,$2,$3) {return $2 ? $1 + ", " + $2 : " " + $3 ;});
}
function testCase3(str) {
let i = 1;
return str.replace(/\B([A-Z])/g, function(match, $1) {
return ( i++ == 1 ? ', ' : ' ' ) + $1;
});
}
strings.forEach(str => {
console.log(str);
console.log('testCase1 : ' + testCase1(str));
console.log('testCase2 : ' + testCase2(str));
console.log('testCase3 : ' + testCase3(str));
});

Sort based on string "weight"

I searched on the internet for a sort algorithm which sorts the entries of a table in a "natural" way so 10 is after 1 and so on but I found not one solution which is "the perfect fit".
Now I am working on my own solution to do a sort algorithm. My question is now if it is efficient and applicable to create a number for each string in a table row based on the position of their letters in the abc and the position of this letter in the specific string and then sort by this created number?
Example:
abc would be a = 1; b = 2; c= 3 and then the "weight" for each letter should be higher with the string length:
a = 1 * 1(pos in str); b = 2 * 2; c = 3 * 3
so abc would be 14 as a number.
I don't want this to be a real natural sort.
I have used libraries and the built in Javascript function ".sort()" but they are not working for strings like 92593c17-5183-4db1-b4bd-d538abb4124bor ed06d686-8a04-4ae1-9500-975fb85a49d9 so it is not the right thing for me.
So is it a good way to sort strings by their letter-"weight"?
Have a look at this little example with localeCompate():
var s1 = '92593c17-5183-4db1-b4bd-d538abb4124';
var s2 = 'ed06d686-8a04-4ae1-9500-975fb85a49d9';
var s3 = '10';
var s4 = '1';
var s5 = 'a';
var s6 = 4;
$('#1').text(s1 + ' before ' + s2 + ': ' + (s1.toString().localeCompare(s2.toString()) == -1 ? 'yes' : 'no'));
$('#2').text(s2 + ' before ' + s3 + ': ' + (s2.toString().localeCompare(s3.toString()) == -1 ? 'yes' : 'no'));
$('#3').text(s3 + ' before ' + s4 + ': ' + (s3.toString().localeCompare(s4.toString()) == -1 ? 'yes' : 'no'));
$('#4').text(s4 + ' before ' + s5 + ': ' + (s4.toString().localeCompare(s5.toString()) == -1 ? 'yes' : 'no'));
$('#5').text(s5 + ' before ' + s6 + ': ' + (s5.toString().localeCompare(s6.toString()) == -1 ? 'yes' : 'no'));
$('#6').text(s6 + ' before ' + s1 + ': ' + (s6.toString().localeCompare(s1.toString()) == -1 ? 'yes' : 'no'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id='1'></p>
<p id='2'></p>
<p id='3'></p>
<p id='4'></p>
<p id='5'></p>
<p id='6'></p>

Change countdown when refresh the page?

I have my own webshop and on the home page i have a lot of products with countdown.
So whats the problem?
The problem is, when i have many javascripts on home page then the broser will freezing, getting slow.
How i know this is an javascript problem ? I tried to disable javascript on the browser and my website works normal.
/*!
*
* Brian2000: BK_Countdown v1.1
* AKA: Brian's jQuery Robust Date/Time Countdown
* http://brian2000.com
*
* Copyright 2012-2013, Brian Kennedy
* Licensed under the GPL Version 3 license
* http://www.gnu.org/licenses/gpl-3.0.en.html
*
* Date: Wed Jan 9 2013 2:54PM EST
*
* You can't remove this part, and if you make changes or improve things, please keep me informed.
* Thank you, enjoy, and support Open Source!
*/
/*
This portion explains how to use the counter, I recomend not deleting it either ;-)
VARS [required]
------------------------------------------------
container: ID of Element for counter
targetDate: MM/DD/YYYY
targetTime: HH:MM:SS (0-23 Hour) [seconds are optional]
OPTIONS:
------------------------------------------------
order: format/output order
order: 1 = Label + Spacer + Value
order: 2 = Value + Spacer + Label (reverse from order 1)
spacer: text/string seperator between label and value
element: html element for label and value containers (default is span)
end: Message to display when date has passed
dayOf: Message to display on day of counter expiration
*/
function BK_CountDown(container, targetDate, targetTime, opts) {
/////////////////////////////////////////////////////
//vars
this.opts = opts;
this.complete = false; //for exiting interval
this.container = container; //target
DArray = targetDate.split('/');
this.targetDate = DArray[0] + '/' + DArray[1] + '/' + DArray[2];
TArray = targetTime.split(':');
this.targetHour = TArray[0]; //hr
this.targetMin = TArray[1]; //min
if (typeof TArray[2] == 'undefined') { //sec
this.targetSec = 0;
}else{
this.targetSec = TArray[2];
}
/////////////////////////////////////////////////////
// options
var defaults = {
'order' : "1",
'spacer' : ':',
'element' : 'span',
'end' : "Deal is ended!",
'dayOf' : "Deal is ended!"
}
if(typeof this.opts != "undefined") { //if options were assigned...
for(var i in defaults) { //assign defaults for unchanged opts
if(typeof this.opts[i] == "undefined") {
this.opts[i] = defaults[i];
}
}
}else{ //no options were assigned
this.opts = defaults;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//////// assembly
/////////////////////////////////////////////////////
s = this.opts['spacer'];
this.c = container.substring(1);
e = this.opts['element'];
if(this.opts['order'] == 2){
//reverse assembly
$(container).append('<' + e + ' id="' + this.c + '_count_days" class="count_days"></' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_txt_days" class="txt_days">' + s + ' Days</' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_count_hours" class="count_hours"></' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_txt_hours" class="txt_hours">' + s + ' :</' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_count_min" class="count_min"></' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_txt_min" class="txt_min">' + s + ' :</' + e + '>');
}
else{
//default assembly
$(container).append('<' + e + ' id="' + this.c + '_txt_days" class="txt_days">Days ' + s + '</' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_count_days" class="count_days"></' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_txt_hours" class="txt_hours">Hours ' + s + '</' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_count_hours" class="count_hours"></' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_txt_min" class="txt_min">Minutes ' + s + '</' + e + '>');
$(container).append('<' + e + ' id="' + this.c + '_count_min" class="count_min"></' + e + '>');
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//////// Count Update function
this.count_update = function count_update(){
date = new Date(); //NOW
targetDate = new Date(this.targetDate);
targetDate.setHours(this.targetHour);
targetDate.setMinutes(this.targetMin);
targetDate.setSeconds(this.targetSec);
var UDate = Math.round(date.getTime()/1000);
var UTargetDate = Math.round(targetDate.getTime()/1000);
differance = UTargetDate - UDate;
days=Math.floor(differance/(60*60*24)*1);
hours=Math.floor((differance%(60*60*24))/(60*60)*1);
minutes=Math.floor(((differance%(60*60*24))%(60*60))/(60)*1);
seconds=Math.floor((((differance%(60*60*24))%(60*60))%(60))*1);
//if range is 0 don't display range
//days
if(days <= 0){$(this.container + '_count_days').remove();$(this.container + '_txt_days').remove();}
else{$(this.container + '_count_days').text(days);}
//hours
if(days <= 0 && hours <= 0){$(this.container + '_count_hours').remove();$(this.container + '_txt_hours').remove();}
else{$(this.container + '_count_hours').text(hours);}
//min
if(days <= 0 && hours <= 0 && minutes <= 0){$(this.container + '_count_min').remove();$(this.container + '_txt_min').remove();}
else{$(this.container + '_count_min').text(+minutes);}
//seconds
$(this.container + '_count_sec').text(seconds);
//Singular text for 'reverse' assembly
if(this.opts['order'] == 2){
if(days <= 1){$(this.container + '_txt_days').text(this.opts['spacer'] + ' dag en ');}
else{$(this.container + '_txt_days').text(this.opts['spacer'] + ' dagen en ')}
if(hours == 1){$(this.container + '_txt_hours').text(this.opts['spacer'] + ' uur en ');}
else{$(this.container + '_txt_hours').text(this.opts['spacer'] + ' uur en ');}
if(minutes == 1){$(this.container + '_txt_min').text(this.opts['spacer'] + ' min');}
else{$(this.container + '_txt_min').text(this.opts['spacer'] + ' min');}
if(seconds == 1){$(this.container + '_txt_sec').text(this.opts['spacer'] + '');}
else{$(this.container + '_txt_sec').text(this.opts['spacer'] + '');}
}
//end of countdown
if(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0){
//timer is over, final output
//if date is today!
if(new Date().toDateString() == targetDate.toDateString()){
$(this.container).addClass('count_now');
$(this.container).text(this.opts['dayOf']);
}else{//if date is after today
$(this.container).addClass('count_end');
$(this.container).text(this.opts['end']);
}
this.complete = true;
}
};
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//run immediately
this.count_update();
//now loop this every second
var selfobject = this; //scope gets lost within setInterval (see: http://www.vonloesch.de/node/32)
var theCounter = setInterval(function(){
selfobject.count_update();
if(selfobject.complete == true){
clearInterval(theCounter);}
}, 1000);
}
HTML:
$(document).ready(function() {
var aanbiedingcountdown = new BK_CountDown('#deal1', '05/25/2014', '23:57', {order: 2, spacer: ''});
var aanbiedingcountdown = new BK_CountDown('#deal2', '05/26/2014', '23:57', {order: 2, spacer: ''});
var aanbiedingcountdown = new BK_CountDown('#deal3', '05/28/2014', '23:57', {order: 2, spacer: ''});
var aanbiedingcountdown = new BK_CountDown('#deal4', '05/28/2014', '15:10', {order: 2, spacer: ''});
var aanbiedingcountdown = new BK_CountDown('#deal5', '05/26/2014', '23:57', {order: 2, spacer: ''});
});
You see, for every countdown that i want i need to create an ID.
But what i want is that when the page load, the countdown dont need to run, it makes my website slower. So, only when the someone refresh the page then he get the right date & time.
Example: Time is now 16:00u, the countdown are set at 17h, so, when someone is still on my website (10min) without refreshing the page then the countdown is still 16h only if he refresh the page then countdown is 16:10h...
I've seen this on other websites and they have more than 100 products with countdowns on the same page and still works perfect because timer is not running only if people refresh the page the the timer will set to right date & time.
Demo: http://jsfiddle.net/uJk73/ (i removed seconds, i though site will go faster without this, but no succes)
Hope someone can help my with this.
Updated jsfiddle
So let's say you had 9 deals
<div id="countdowntimer">
<div id="deal1"></div>
<div id="deal2"></div>
<div id="deal3"></div>
<div id="deal4"></div>
<div id="deal5"></div>
<div id="deal6"></div>
<div id="deal7"></div>
<div id="deal8"></div>
<div id="deal9"></div>
</div>
Get rid of this loop in BK_CountDown()
//now loop this every second
/*var selfobject = this; //scope gets lost within setInterval (see: http://www.vonloesch.de/node/32)
var theCounter = setInterval(function(){
selfobject.count_update();
if(selfobject.complete == true){
clearInterval(theCounter);}
}, 1000);
*/
Then
$(document).ready(function() {
var deals = []; // create an array
deals.push( new BK_CountDown('#deal1', '05/24/2014', '23:59', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal2', '05/24/2014', '22:59', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal3', '05/24/2014', '12:59', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal4', '05/24/2014', '13:49', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal5', '05/24/2014', '14:49', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal6', '05/24/2014', '15:57', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal7', '05/24/2014', '15:58', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal8', '05/24/2014', '15:59', {order: 2, spacer: ''}) );
deals.push( new BK_CountDown('#deal9', '05/24/2014', '16:00', {order: 2, spacer: ''}) );
var dealcounter = setInterval(function(){ // create one setTimeout
if (deals.length){
for (i in deals){
var selfobject = deals[i];
selfobject.count_update();
if(selfobject.complete == true) delete deals[i]; // remove from array
}
}else{
clearTimeout(dealcounter); // no more to count down, stop this loop
}
}, 5000); // 5 seconds
});

Categories