I would like to add a delay of 5 seconds after likeMe() loop has finished. Once finsh I would like to trigger a click on the log out button of Facebook.
I'm stupid when it comes to javascript symantics; therefore I have no idea on how to trigger any of this? With this in mind where/how would I add this effect after the loops is over:
I've normally use .promise().done() to do the finally steps after a loops but I have no idea how to place this $.delay(5000).promise().done( $('input[value="Log Out"]').click()); within the javascript.
BACKGROUND: creating a installation art piece using arudino, processing and & greasemonkey jQuery.
FULL SCRIPT:
unsafeWindow.likeMe = likeMe;
function likeMe()
{
input = document.getElementsByTagName("input");
for(i = 0; i < input.length; i++)
{
myID = input[i].getAttribute("data-profileid");
if(myID != null && myID.indexOf("342584172457437") >= 0)
input[i].click();
}
$.delay(5000).promise().done( $('input[value="Log Out"]').click());
// this is wrong but yeah i have no clue where or how to put it on here.
}
$(window).load(function(){
var isCtrl = false;
document.onkeyup=function(e) {
if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 46 && isCtrl == true) {
likeMe()
return false;
}
}
});
You can use setTimeout like
function likeMe()
{
input = document.getElementsByTagName("input");
for(i = 0; i < input.length; i++)
{
myID = input[i].getAttribute("data-profileid");
if(myID != null && myID.indexOf("342584172457437") >= 0)
input[i].click();
}
setTimeout(function() {
$('input[value="Log Out"]').click();
}, 5000);
}
or inside the onkeydown like
document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 46 && isCtrl == true) {
likeMe();
setTimeout(function() {
$('input[value="Log Out"]').click();
}, 5000);
return false;
}
}
But not in both at the same time.
Use setTimeout() like this:
setTimeout(function() {
$('input[value="Log Out"]').click();
}, 5000);
to execute a function in five seconds.
Related
I have a function which alert user if he enters less than or more than chars.. in the line of textarea.
In addition to that I'd like to disable "Enter button" e.which == 13 if condition is not meeting requirements.
I wonder how I can disable/enable enter button e.which == 13 if some of requirements are not met. Thanks
$('#customnumbers').keypress(function(e) {
if(e.which == 13) {
var text = $(this).val();
var lines = text.split(/(\r\n|\n|\r)/gm);
for (var i = 0; i < lines.length; i++) {
if (lines[i] != '\n' && (lines[i].length < 7 || lines[i].length > 15)) {
alert('Number must be more than 7 and less than 15 characters.');
}
}
}
});
can you try this code, by showing a div message instead of an alert :
$('#customnumbers').keypress(function(e){
if (e.keyCode === 10 || e.keyCode === 13)
var text = $(this).val();
var lines = text.split(/(\r\n|\n|\r)/gm);
for (var i = 0; i < lines.length; i++) {
if (lines[i] != '\n' && (lines[i].length < 7 || lines[i].length > 15)) {
var myMessageDiv=$('<div class="myMessageDiv">Number must be more than 7 and less than 15 characters.</div>');
myMessageDiv.insertBefore( "#customnumbers" );
setTimeout(function(){ $('.myMessageDiv').remove(); }, 3000); // after 3 seconds remove message.
e.preventDefault();
return false;
}
}
}
});
You can disable the enter key this way:
$('body').bind('keypress', function(e){
if(e.keyCode == 13){
return false;
}
});
Hope it helps.
I've an textbox in asp.net,when i enter ";" semicolon in textbox means it have to call a function.Is there any way to do this.please help me out guys..i tried change function but it calls at every keypress in textbox.
$('#prgrp').on('change', function (evt)
{
var txt = $("#prgrp").val();
var valueArray = txt.split(';');
var valueSortArray = valueArray.sort();
var duplicateValues = [];
for (var i = 0; i < valueSortArray.length; i++)
{
if (valueSortArray[i + 1] == valueSortArray[i])
{
duplicateValues.push(valueSortArray[i]);
}
}
if (duplicateValues.length > 0)
{
$("#duplicate").html("Don't enter repeated values");
$('#duplicate').css('color', 'RED');
$("#prgrp").autocomplete("disable");
}
else {
$("#duplicate").html("");
$("#prgrp").autocomplete("enable");
}
});
Try this:-
$("#prgrp").keypress(function (e) {
if (e.keyCode == 59) {
//Call your function here
}
});
Please note, you can also use e.which in place of e.keyCode as it is jquery standardized.
Try this
$('#prgrp').bind('keypress', function(e) {
var code = e.keyCode || e.which;
if(code == 59) { //
//Do something
}
});
Demo
try this code
$( "#prgrp" ).keypress(function( event ) {
if ( event.which == 59 || event.keycode == 59 ) {
//your function call
}
});
I have a piece of JavaScript which I use to search my site. When you type into the search it pulls up the products matching you query. This works fine, but I want the dropdown to close when the user clicks on the page. I know very little about JavaScript so any advice or even a point in the right direction will be appreciated.
function showSmartSearch() {
$.ajax({
url: "index.php?route=product/ajaxsearch&search=" + $("input[name='search']").val(),
type: "GET",
success: function(e) {
$("#smartsearchdiv").css("display", "block");
var t = "";
$("#smartsearchdiv").html(e)
}
})
}
function hideSmartSearch() {
$("#smartsearchdiv").css("display", "none")
}
var wait;
$(document).ready(function() {
$("input[name='search']").after('<div id="smartsearchdiv" style="margin-top:30px;background:white;width:230px;position:absolute;z-index:999;"></div>').blur(function() {}).keydown(function(e) {
if ($("input[name='search']").length && e.which == 38) {
e.preventDefault();
return false
}
}).keyup(function(e) {
if (!$("input[name='search']").val()) {
$("#smartsearchdiv").css("display", "none")
}
var t = $("input[name='search']").val();
var n = t.length;
if (t.replace(/^\s+|\s+$/g, "") && (e.which == 8 || 47 < e.which && e.which < 112 || e.which > 185)) {
if (n > 0) {
showSmartSearch()
} else {
hideSmartSearch()
}
}
if ($("#smartsearchdiv li").length && (e.which == 38 || e.which == 40)) {}
}).blur(function() {})
});
function hideSmartSearch() {
$("#smartsearchdiv").css("display", "none");
}
You can try this:
$(document).click(function(e){ // hide list if clicked outside
if($(e.target).is('#smartSearchDiv, #smartSearchDiv *'))return;
hideSmartSearch();
});
Also this:
$(document).click(function(e){ // hide list if clicked outside
if(!$(e.target).is('#smartSearchDiv'))
hideSmartSearch();
});
Use this:
$("#smartSearchDiv").click(function(e){
e.stopPropagation();
});
$(body).click(function(){
if($("#smartSearchDiv").is(":visible")){
hideSmartSearch();
}
});
This way, if you click on your search div, event will stop propagation to its parents, and nothing will happen. Otherwise, it will go upto your body and fire a close function.
I have this jquery code to handle some cases if user clicked right or left button but the navigate became a mess when the user click both buttons together
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39){
setTimeout(RightKeyPressed(),5000);
}
});
});
I want to simply block any call till the first or second function completely executed
Use a flag to block the unnecessary function calls like this,
$(document).ready(function(){
var xKeyPressed = false;
$(document).keydown(function(e){
if (e.keyCode == 37 && !xKeyPressed) {
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 !xKeyPressed){
setTimeout(RightKeyPressed(),5000);
}
});
function LeftKeyPressed()
{
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
function RightKeyPressed(){
xKeyPressed = true;
//Your code
xKeyPressed = false;
}
});
Set a simple check if your code is running LeftKeyPressed or RightKeyPressed like this:
var inFunction = false;
$(document).ready(function(){
$(document).keydown(function(e){
if (e.keyCode == 37 && !inFunction) {
inFunction = true;
setTimeout(LeftKeyPressed(),5000);
}
else if(e.keyCode == 39 && !inFunction){
inFunction = true;
setTimeout(RightKeyPressed(),5000);
}
});
});
//Add this in your functions, both Left and Right variants,
function LeftKeyPressed(){
//Do your normal stuff here
inFunction = false;
}
There is other ways of doing this as well, but this is in my opinion a clear and concise way of doing it. You can potentially get race conditions with this approach.
$(document).ready(function(){
var navt;
$(document).keydown(function(e){
if(navt != null) return;
if (e.keyCode == 37) {
navt = setTimeout(function(){ LeftKeyPressed(); navt = null; },5000);
}
else if(e.keyCode == 39){
navt = setTimeout(function(){ RightKeyPressed(); navt = null; },5000);
}
});
});
I'm trying to implement a Tab key listener for a textbox.
$('#mytextbox').live('keydown', function (e) {
if (e.keyCode == 9 || e.which == 9) {
// TO DO SOMETHING
}
});
However, for some reason I need to limit the tab listener's callback to invoke only when the textbox has changed. Is there anyway to do this?
You might be able to check the value of the input field to make sure it's different from it's original value?
E.g.
$('#mytextbox').live('keydown', function (e) {
if ((e.keyCode == 9 || e.which == 9) && ($('#TextBox').val() != 'Starting Value')) {
// TO DO SOMETHING
}
});
You can do it just like:
var data = "";
$('#mytextbox').live('keydown', function (e){
if(e.which == 9 || e.keyCode == 9){
if($(this).val() != data){
alert('changed!');
data = $(this).val();
}
}
});
http://jsfiddle.net/DDCZS/1/
Or without storing / knowing value of that textbox:
var changed = false;
$('#mytextbox').on('keydown', function (e) {
if (e.which == 9 && changed) {
e.preventDefault();
// TO DO SOMETHING
alert("works");
changed = false;
} else {
changed = true;
}
});
http://jsfiddle.net/9a37b/