Javascript clear array was working but suddenly it's not - javascript

Iam working in cordova with cordova-local-notifications plugin. First the clear function was working but now when I clear and restart the application the notification all show up...
This is my clear function:
$('.clearbtn').click(function(){
var arraylength = info.data;
arraylength.splice(0,arraylength.length);
console.log('clear',arraylength);
});
The console log shows me that the array has nothing inside it. And this is the outcome of the console.log(info.data)
and this is the all reminder page:
$(document).on("pagebeforeshow","#all",function(){
var html = '';
for(var count = 0; count < info.data.length; count++){
console.log(info.data);
var time = new Date(info.data[count][3]);
var addHour = time.setHours(time.getHours());
var date = new Date(addHour);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2);
var imgPath = 'img/icons/alert.png';
html = html + "<tr><td class='img-table'><img src=" + imgPath + "></td>" + "<td><span class='time-aside'><strong>" + formattedTime + "</strong></span><h4>" + info.data[count][1] + "</h4><p>" + info.data[count][2] + "</p></td></tr>";
}
$('.clearbtn').click(function(){
var arraylength = info.data;
arraylength.splice(0,arraylength.length);
console.log('clear',arraylength);
});
$("table#notificationTable tbody").empty();
$("table#notificationTable tbody").append(html).closest("table#notificationTable").table("refresh").trigger("create");
});
I really don't get it because it was working before but I don't know why its not anymore!

I had to clear localStorage aswell.. Because I push it to localstorage as string. Here is a rewrite of the function:
$('.clearbtn').click(function(){
var arraylength = info.data;
arraylength = 0;
localStorage.clear();
});

Related

document.lastModified only displays current date?

I've been looking for a way to display the date the page last was updated.
Now I've been searching around, and everything points to the document.lastModified function, but however I've tried to fix it, it always shows the current date.
I've tried this example:
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" + modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds(); }
else {
Seconds = modiDate.getSeconds(); }
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime }
document.write("Last updated on ");
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]"); document.write("");
Or a simple one like this:
<SCRIPT LANGUAGE="JavaScript">
var t = new Date(document.lastModified);
document.write("<I>Last Updated: "+document.lastModified+"</I><BR>");
document.write("<I>Last Updated: "+t+"</I><BR>");
</SCRIPT>
Is there any other way to do this?
.. Without taking a 3 years tech-class?
Press here to see the scripts live
Because you are modifying it currently. Check this out for example.
To make this work based on your requirement, checkout this link and this link
check this it will help u
Put this on the page at the bottom:
<script type="text/javascript" src="js_lus.js"></script>
Name the file whatever you want. Example: js_lus.js Make sure src=""
path is correct for all your pages.
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" +
modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds();
} else {
Seconds = modiDate.getSeconds();
}
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime
}
document.write("Last updated on ")
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");

javascript coundown " SyntaxError: expected expression, got '.' "

var dateObj = new Date();
var month = dateObj.getUTCMonth() +1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var nowhour = dateObj.getHours();
var nowday = dateObj.getUTCDate();
var hour = "03";
var min = "00";
var hour2 = "18";
var min2 = "00";
var hour3 = "21";
var min3 = "00";
if(hour == 03)
{
day++;
}
document.write(nowhour);
newdate = year + "/" + month + "/" + day;
hourdate = " " + hour + ":" + min;
hourdate2 = " " + hour2 + ":" + min3;
hourdate3 = " " + hour2 + ":" + min3;
$("#bifrost")
if(nowhour > hour && day > nowday)
{
.countdown(newdate + hourdate, function (event) {$(this).text(event.strftime('%H:%M:%S'));});
}else if(nowhour > hour2)
{
.countdown(newdate + hourdate2, function (event) {$(this).text(event.strftime('%H:%M:%S'));});
}else{
.countdown(newdate + hourdate3, function (event) {$(this).text(event.strftime('%H:%M:%S'));});
}
Hello, i wanna make a countdown timer for events. I have 3 different event time,i wanna show up coming event here is my javascript code.
can anyone help me ?
ps: sorry for my bad english.
If countdown is a global function, you do not need the period before the call, just
countdown(parameters ... );
If countdown is a jquery plugin you have, and you are trying to call it on the jQuery object you created before the if statements, you must do it like this
$("#bifrost").countdown(parameters ... );
And repeat the jQuery selector in each of your if statements.
There is built in function setTimeout(function,milliseconds,param1,param2,...). Please see for examples in here.
setTimeout(function_to_do, miliseconds to wait) - will be triggered once;
setInterval(function_to_do, miliseconds to wait) - will be triggered periodically.
By the way - function name can't start with dot - and you have three calls to something .countdown(... There is you error.

Google Apps Script: "Missing ';' before statement"

Today is my first day using Google Apps Script. I am trying to make a script to grab weather information from the Wunderground Api and paste it into a spreadsheet.
For some reason I am getting the error message "Missing ';' before statement. (Line 34)".
I've searched for a solution but cant find why I am getting this error in my code.
//Everyday this script gets weather data from Weather Underground and records it in this spreadsheet.
cDay = 0, cTemp = 1, cHumidity = 2, cPressure=3, cSparkline=4, cConditions=5;
nCols=7;
function getTemp() {
var url = 'http://api.wunderground.com/api/' + appKey + '/conditions/q/CO/Aspen.json';
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var response = UrlFetchApp.fetch(url);
var contentText = response.getContentText();
var conditions = Utilities.jsonParse(contentText);
var todaysConditions = conditions;
var temp = todaysConditions.current_observation.temp_c;
var humidity = todaysConditions.current_observation.relative_humidity;
var pressure = todaysConditions.current_observation.pressure_in;
var conditions = todaysConditions.response.features.conditions;
sheet.insertRowAfter(1);
var range = sheet.getRange(2,1,1, nCols);
var row = range.getValues()[0];
var d = new Date;
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();
var hour = d.getHours() + 1;
var minutes = d.getMinutes();
row[cDay] = month + '/' + day + '/' + year + '' + hour + ':' minutes; //here is the error
row[cTemp] = temp;
row[cHumidity] = humidity;
row[cPressure] = pressure;
row[cConditions] = conditions;
var nRows = numRows >= 10 ? 10 : numRows;
//row[cSparkline] = "=SPARKLINE(R[0]C[-3]:R[" + (nRows-1) + "]C[-3])";
range.setValues([row]);
}
Any help is appreciated
You have missed the '+' sign
row[cDay] = month + '/' + day + '/' + year + '' + hour + ':' + minutes;

Javascript runs in browser, not in phonegap app with cordova

This code below is for a form that needs to save some values into local storage, I have got it to work in a browser, but when I load this thing up in xcode/cordova, it wont fire the function. I tried debugging, hence the many, many alerts, I tried in DWCS6 with live view, but no avail, I can't seem to get it to run. Can you please find the error?
The function saveSpanning() has an if loop and when it goes out of the if-then-else loop it doesn't continue in xcode simulator. In the browser it does continue.
update: what is does: there's a slider, that produces a value, this value needs to be saved in local storage along with two other values (generated by JavaScript), namely: the date and the record count. So three values in all.
The script runs, using jquery mobile, the button to start this function works, I have use document.ready instead onBodyLoad, it basically works, but the function saveSpanning just does not go further in phonegap/ios/xcode simulator or device.
function saveSpanning() {
alert("saveSpanning gestart!");
var inputSpanning = document.getElementById("valSliderSpanning").value;
alert("input spanning = " + inputSpanning);
//For Time
var mes_time = document.getElementById("tijdSpanning").value;
var mestimearr = mes_time.split(":");
//For Date
var mes_date = document.getElementById("datumSpanning").value;
var mesdatearr = mes_date.split("-");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hours = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();
//newDate = curr_year + "/" + curr_month + "/" + curr_date + " " + curr_hours + ":" + curr_min + ":" + curr_sec
// origienel opmaak datum newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2] + " " + mestimearr[0] + ":" + mestimearr[1] + ":00";
newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2];
alert("deze datum wordt opgelsage: " + newDate);
//var itemId = newDate.getTime(); //creates a unique id with the milliseconds since January 1, 1970
var itemId = "spanningKey";
var values = new Array();
values.push(newDate); //push each value into our values array
values.push(inputSpanning); //push each value into our values array
//alert(inputSpanning);
var spanningCountVal = localStorage.getItem('spanning_count');
//alert(spanningCountVal);
if (spanningCountVal == null) {
spanningCountVal = 1;
alert("spanningCountVal was null, en wordt dus nu 1: " + spanningCountVal);
}
else {
spanningCount = parseInt(spanningCountVal) + 1;
alert("zit nu in de else loop: " + spanningCount);
}
alert("uit de ifthenelseloop, spanningCount = " + spanningCount);
itemId = itemId + '-rec-' + spanningCount;
alert("itemid: " + itemId);
alert("spanningCountVal: " + spanningCount);
localStorage.setItem("spanning_count", spanningCount); //store the item in the database
localStorage.setItem(itemId, values.join("|")); //store the item in the database
alert("Successfully Saved.");
}
$(document).ready(function() {
$("#button").click(function() {
alert("hallo functie");
});
$("p").text("The DOM is now loaded and can be manipulated.");
$('#button2').click(function() {
alert('Button has been clicked');
});
$('#knopje').click(function() {
saveSpanning();
});
});​
document.ready is called when all the DOM Elements are loaded, and accessible. In a browser this typically is a good time to start executing code that requires the DOM to be ready.
However in Phonegap/Cordova, there are a number of steps that run after the document.ready event is fired, including connecting to the debugging console.
Instead, you'll need to wait for the deviceready event to know when phonegap/cordova is fully loaded and ready to be executed. Inside $(document).on('ready') you'll need to add an event listener for deviceready which should fire your methods.

Modify Script to Include DIV around AM/PM

How can I modify this script to encase the AM/PM text in a div?
I already tried modifying the areas where it outputs the ampm with a div, but it didnt put the element in a div, instead displaying the and tags as plaintext.
function myTime() {
var dst = 0; // set to 1 for daylight savings time
// update this as you go on and off daylight saving time
var loc = ''; // set to your location
var mtz = -5; // set to your local timezone (hours ahead of UTC, negative if behind)
var stdz = ''; // standard time indicator
var dayz = ''; // daylight saving time indicator (blank if you dont have daylight saving)
var showDate = 0; // 0 = don't show, 1 = international format, 2 = US format
// do not alter anything below this line
var newP = document.createElement("div"); var txt = '' + loc + ' '; var newT = document.createTextNode(txt); newP.appendChild(newT); var newP2 = document.createElement("div"); newP2.id = 'time'; var txt2 = setDsp(mtz,dst,stdz,dayz,showDate); var newT2 = document.createTextNode(txt2); newP2.appendChild(newT2); var frag = document.createDocumentFragment(); frag.appendChild(newP); frag.appendChild(newP2); var d2 = document.getElementById('datetime'); d2.parentNode.replaceChild(frag,d2);
setTimeout('updDsp('+mtz+',' +dst+',"' +stdz+'","' +dayz+'","'+showDate+'")', 5000);}
var pageLoaded = 0; window.onload = function() {pageLoaded = 1;}
function loaded(i,f) {if (document.getElementById && document.getElementById(i) != null) f(); else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
function updDsp(mtz,dst,stdz,dayz,showDate) {var obj = document.getElementById('time'); obj.firstChild.data = setDsp(mtz,dst,stdz,dayz,showDate); setTimeout('updDsp('+mtz+ ','+dst+ ',"'+stdz+ '","'+dayz+ '","'+showDate+'")', 5000);}
function setDsp(mtz,dst,stdz,dayz,showDate) {var dayname = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday']; var month = ['January','February','March','April','May','June','July','August','September','October','November','December']; var now = new Date; now.setUTCMinutes(now.getUTCMinutes() + (mtz + dst)*60); var dow = now.getUTCDay(); var minute = now.getUTCMinutes();
var hour = now.getUTCHours(); if (hour > 11) {ampm = 'PM'; hour -= 12;} else {ampm = 'AM'} if (hour == 0) {hour = 12;} if (minute < 10) {pad = ':0';} else {pad = ':';} var txt = hour + pad + minute + ' ' + '' + ampm + ''; if (dst) txt += dayz; else txt += stdz; txt += ' '
if (showDate == 1) txt += ' ' + now.getUTCDate() + ' ' + month[now.getUTCMonth()] + ', ' + now.getUTCFullYear();
if (showDate == 2) txt += ' ' + month[now.getUTCMonth()] +' ' + now.getUTCDate() + ', ' + now.getUTCFullYear();
return (txt);
}
loaded('datetime',myTime);
Your code is way over complex. I'd suggest replacing the myTime() function with this that just builds HTML rather than create all the various text nodes. This also puts the AM or PM designation in a <span class="ampm"> so it can be styled appropriately:
function myTime() {
var dst = 0; // set to 1 for daylight savings time
// update this as you go on and off daylight saving time
var loc = ''; // set to your location
var mtz = -5; // set to your local timezone (hours ahead of UTC, negative if behind)
var stdz = ''; // standard time indicator
var dayz = ''; // daylight saving time indicator (blank if you dont have daylight saving)
var showDate = 0; // 0 = don't show, 1 = international format, 2 = US format
var timeStr = setDsp(mtz, dst, stdz, dayz, showDate);
timeStr = timeStr.replace(/AM|PM/i, '<span class="ampm">$&</span>');
var d = document.getElementById('datetime');
d.innerHTML = '<div id="time">' + loc + " " + timeStr + '</div>';
setTimeout(myTime, 500);
}
And here's a working version: http://jsfiddle.net/jfriend00/jTbf2/

Categories