Countdown with callback with jQuery - ajax

I use Rendro countdown (http://github.com/rendro/countdown/)
$('.countdown.callback').countdown({
date: +(new Date) + 10000,
render: function(data) {
$(this.el).text(this.leadingZeros(data.sec, 2) + " sec");
},
onEnd: function() {
$(this.el).addClass('ended');
}
}).on("click", function() {
$(this).removeClass('ended').data('countdown').update(+(new Date) + 10000).start();
});
Styled output
<div class="countdown styled"></div>
<h2>Countdown with callback</h2>
<p>Click on the green box to reset the counter to 10 sec.</p>
<div class="countdown callback"></div>
How to automatically reset the counter (without click) back to 10 after countdown = 0 ?
I tried insert onclick function into onEnd but doesn't works.

Try resetting the date option and calling the start([refreshRate]) method in the onEnd function after you do your addClass.
this should restart the countdown.
something like this:
$('.countdown.callback').countdown({
date: +(new Date) + 10000,
render: function(data) {
$(this.el).text(this.leadingZeros(data.sec, 2) + " sec");
},
onEnd: function() {
$(this.el).addClass('ended');
this.update(+(new Date) + 10000);
this.start(1000);
}
}).on("click", function() {
$(this).removeClass('ended').data('countdown').update(+(new Date) + 10000).start();
});
see a fiddle here: http://jsfiddle.net/MaurizioPiccini/xDy5d/

Related

get clicked date in bootstrap-datepicker

I try to get the date clicked in the bootstrap-datepicker. This is the code so far, I retrieve the date that is selected and not the date that is clicked. What do I need to change to get the clicked date?
<script type="text/javascript">
var disableDates = ["1-3-2022", "3-3-2022"];
$('#calendar').datepicker({
format: 'yyyy/mm/dd',
language: "sv",
beforeShowDay: function(date){
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if(disableDates.indexOf(dmy) != -1){
return false;
}
else{
return true;
}
},
dayClick: function (date, jsEvent, view) {
alert("Day Clicked");
},
eventClick: function (event) {
alert('event');
}
});
$("#calendar .datepicker-days").on('click', 'td.day', function () {
let selecteddate = $("#calendar").data('datepicker').getFormattedDate('yyyy-mm-dd');
alert('day clicked ' + selecteddate);
});
</script>

Ajax call after 10 second automatic inside a loop

jQuery:
// Load message
(function loadAdmin() {
$.ajax({
type: 'POST',
data: 'c_id=' + $(this).data('c_id') + '&offset=' + $(this).data('offset'), //'foo='+ bar+'&calibri='+ nolibri,
dataType: 'json',
url: $("#webroot").text() + 'chats/loadMsg',
success: function (data) {
var id = 0;
$.each(data, function (i, item) {
if (item.Chat.status == 'active') {
$('.temp_msg').remove();
}
if (!$('#' + item.Chat.id)[0]) {
if (item.Chat.admin_message) {
$('<div class="msg_a" id="' + item.Chat.id + '">' + item.Chat.admin_message + '</div>').insertBefore('.' + $(this).data('c_id') + ' .msg_push');
}
if (item.Chat.client_message) {
$('<div class="msg_b" id="' + item.Chat.id + '">' + item.Chat.client_message + '</div>').insertBefore('.' + $(this).data('c_id') + ' .msg_push');
}
$('.msg_body').scrollTop($('.msg_body')[0].scrollHeight);
}
id = item.Chat.id;
});
$('.' + c_id + ' .msg_head').data('offset', id)
},
complete: function () {
// Schedule the next request when the current one's complete
setTimeout(loadAdmin, 10000);
}
});
})();
// END load message
I set c_id statically.This works fine. But I want to do this same action for all activated class.
This is something like this:
$('.activated').each(function () {
// do ajax call
loadAdmin();
});
Here crucial situation is for first iteration there start a recursion of loadAdmin(); function calling, then again start first iteration and never step up to second iteration. How can I meet my situation. Thanks
you can use setInterval() function for complete your requirements.
Like this :
$('.activated').each(function () {
// do ajax call
setInterval(loadAdmin(), 10000); // This will call this function in every 10 sec
});
Try this. I hope you will found it.

When there is a progress indicator

At the server side, response a string after 5 seconds.
There will be a progress indicator for the first 5 seconds, if use the following codes:
function Comet(){
$.getJSON('http://192.168.1.111:3000/c?callback=?', {}, function(response){
toDiv(response);
setTimeout('Comet()', 1000);
});
}
function toDiv(content){
$('#content').append(content + '<br>');
}
$(document).ready(function() {
Comet();
});
But there will be no progress indicator if using the codes:
function Comet(){
$.getJSON('http://192.168.1.111:3000/c?callback=?', {}, function(response){
toDiv(response);
setTimeout('Comet()', 1000);
});
}
function toDiv(content){
$('#content').append(content + '<br>');
}
$(document).ready(function() {
setTimeout('Comet()', 1000);
});
Why?

.setPostData jqgrid not working

I want to reload jqgrid with new parameters.I'm using .setPostData().Please look at my code below.It always give me error at .setPostData().M I missing something? format?
$('table[id$="'+tabID+'_BBGrid"]').jqGrid({
url:'/Controls/Advertiser/BBControlNew.ascx.ashx?action=getBBData&advertiserID=' + $('#advertiser_id').text() + '&startDate=' + $('input[id$="' + tabID +
'_FromCalBuyBack_CalendarTbx"] ').val() + '&endDate=' + $('input[id$="' + tabID + '_ToCalBuyBack_CalendarTbx"] ').val(),
datatype: 'json',
mtype: 'POST',
height:'100%',
width:'100%',
colNames: result.colNamesData,
colModel: result.colModelData,
//pager: '#RequestLeadspager',
rowNum : 100,
shrinkToFit :false,
...........
function BuyBackGridReload(tabID,NoSelectedValues)
{
$('table[id$="'+tabID+'_BuyBackGrid"]').setPostData({
advertiserID:$('#advertiser_id').text(),
CampaignsDdlSelectedValue: $('select[id$="CampaignDdl"] option:selected').val(),
startDate: $('input[id$="'+tabID+'_FromCalBuyBack_CalendarTbx"] ').val(),
endDate: $('input[id$="'+tabID+'_ToCalBuyBack_CalendarTbx"] ').val(),
NoSelectedValue: NoSelectedValues
}).trigger("reloadGrid");
};
I have search btn.I'm getting values for NoSelectedValues inside that search btn. here is the code for button click.
$('input[id$="'+tabID+'_BuyBackSearchBtn"]').click(function(){
var values = [];
$('div[id$="' + tabID + '_SelectedBuyBackFilterDiv"] .children').each(function (){
$(this).find('option').each(function (){
var attr = $(this).attr('rel');
if (typeof attr == 'undefined' ){
values.push($(this).val());
}
});
});
BuyBackGridReload(tabID,values);
}); //End search click
ERROR:
$("table[id$=\"" + tabID +
"_BuyBackGrid\"]").setPostData({advertiserID:
$("#advertiser_id").text(),
CampaignsDdlSelectedValue:
$("select[id$=\"CampaignDdl\"]
option:selected").val(), startDate:
$("input[id$=\"" + tabID +
"_FromCalBuyBack_CalendarTbx\"]
").val(), endDate: $("input[id$=\"" +
tabID + "_ToCalBuyBack_CalendarTbx\"]
").val(), NoSelectedValue:
NoSelectedValues}) is undefined
I also don't want to pass as a querystring for new parameters.
Any suggestion?
Thanks
A
You don't need to use setPostData to set the postData parameter. You can use setGridParam function instead. See here examples.
I suppose if you will use postData parameter which contain functions you will not need to set any postData parameter at all. The url and postData parameters of jqGrid can look like
url:'/Controls/Advertiser/BBControlNew.ascx.ashx",
postData: {
action: "getBBData"
advertiserID: function() { return $('#advertiser_id').text(); },
startDate: function() { return $('input[id$="' + tabID + '_FromCalBuyBack_CalendarTbx"] ').val(); },
endDate: function() { return $('input[id$="' + tabID + '_ToCalBuyBack_CalendarTbx"] ').val(); },
advertiserID: function() { return $('#advertiser_id').text(); },
CampaignsDdlSelectedValue: function() { return $('select[id$="CampaignDdl"] option:selected').val(); },
startDate: function() { return $('input[id$="'+tabID+'_FromCalBuyBack_CalendarTbx"] ').val(); },
endDate: function() { return $('input[id$="'+tabID+'_ToCalBuyBack_CalendarTbx"] ').val(); },
NoSelectedValue: function() { return NoSelectedValues; }
}
The variables tabID and NoSelectedValues must be defined before. On every grid reloading the function from every postData property will be called and you can read the current values from the corresponding controls.

Jquery UI Autocomplete Ajax JSONP return is broken in JQ UI Version 1.8.6

Ive been using the code below for my auto-completes on my form for a while , but after updating jquery ui to version 1.8.6 from 1.8rc3 it has broken the formatting of the JSONP return. The returned data is no longer formatted html, but instead it is a string. Any Ideas?
Update: JS Fiddle included, using the jquery ui demo and html in the data
http://jsfiddle.net/blowsie/ejLPg/
$("#companyname").autocomplete({
source: function (request, response) {
$.ajax({
url: turl,
dataType: "jsonp",
data: {
maxRows: 9,
name_startsWith: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: "<span class='ui-menu-item-title'>" + item.name.toLowerCase() + "</span><span class='ui-menu-item-subtitle'>" + item.address1.toLowerCase() + ' ' + item.post_code.toLowerCase() + '</span>',
value: item.name_id
}
}))
}
})
},
minLength: 3,
delay: 50,
select: function (event, ui) {
LoadGivenCompany(ui.item.value);
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
},
focus: function () { return false }
});
Thanks in advance
I looked into the source code of autocomplete of version 1.8.6 and found out that item creation uses text method instead of the html method. They suggest at jQuery.ui to change style using the theme roller of by altering the widget specific classes in the css file.
See Autocomplete#theming.
What i would do is find the place that they push the text in the element and change the method call back to html and test it out.
Turns out by using the .data(), after the autocomplete allows you to format the data
$(function() {
function log(message) {
$("<div/>").text(message).prependTo("#log");
$("#log").attr("scrollTop", 0);
}
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name,
test: 'hahahah'
}
}));
}
});
},
minLength: 2,
select: function(event, ui) {
log(ui.item ? "Selected: " + ui.item.label : "Nothing selected, input was " + this.value);
},
open: function() {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function() {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "<br>" + item.test + "</a>" )
.appendTo( ul );
};
});
http://jsfiddle.net/blowsie/ejLPg/3

Resources