This' Does Not Work Properly in Another Event. I'm Clueless as to Why

this' does not work properly in another event. I'm clueless as to why

You aren't attaching the event handler correctly. This line:

$('.figure').click(toggleCarousel(this));

...is calling toggleCarousel with this immediately (that's what the parens will do). What you really want is to pass the function object to .click():

$('.figure').click(toggleCarousel);

Update:

As @FelixKling pointed out, you'll also want to pass the target of the event to the downstream functions; it looks like they expect an element, not the event. Also, bool will be reset to false each call, which is not what you want; you should put it in the closure:

var flag = false; // "bool" is a reserved word, changed the name
function toggleCarousel(event) {
var element = event.target;
if (flag) {
stopCarousel(element);
}
else {
startCarousel(element);
}
flag = !flag;
}

Changing Context of this in JavaScript

If I understand your question correctly, then you can achieve this by means of the following:

function testFunc(num1, num2, f)   {   return function(x, y)   {    // f() is called with context of Obj instance,     // with args x and y     f.call(this, x, y)   } }
function Obj(first, num) { this.first = first; this.num = num;}
Obj.prototype.add = testFunc( 1, 2, function addFunc (x) { // addFunc() called from testFunc(), this is // instance of Obj this.num += x; } ); var ob = new Obj("Joe", 100); console.log('Before', ob.num); ob.add(50); console.log('After', ob.num);

this' binding in Reusable Functions?

Why would 'this' not be set to element #start for instance?

Why would it be? It's not that it can sense what you want it to do.

Use Function#call to define what object this should point to during the call. If you don't explicitly define it, this will default to the global object (Window in browsers).

$(document).ready(function ($) {

function nextCard() {
console.log('yes');
$(this).closest('.card').hide();
$(this).parent().next().show();
}

$("#start").on('click', function (e) {
e.preventDefault();

//First card to appear
nextCard.call(this);
});

$("#next").on('click', function (e) {
e.preventDefault();
nextCard.call(this);
});
});

Using someFunction.call(this); will effectively "transfer the current meaning" of this to the called function, or more technically, call someFunction in the context of whatever object this is referencing at the moment.

jQuery does something like the above automatically - it sets this for you to the proper DOM element when it calls event handlers. You can make use of the automatic this handling like @Rory McCrossan's answer shows – or you handle this yourself.

javascript: Assign function to a variable fills variable with undefined

It looks like you want Sum to be a constructor function. You call constructor functions via new, not directly. So:

var s1 = new Sum();
var s2 = new Sum();

The reason you were getting undefined in s1 and s2 without new is that Sum doesn't do return x anywhere, so the result of calling it is the value undefined. But when you use new, the result of the new expression is the object that was created.

FWIW, there's another problem with Sum: You're missing out this. in a couple of places (also a couple of semicolons, though Automatic Semicolon Insertion will add them for you):

function Sum() {
this.result = 0;

this.getCurrentSum = function getCurrentSum() {
return this.result;
// −−−−−−−−^^^^^
};

this.add = function add(add_number) {
this.result += add_number;
// −^^^^^
};
}

JavaScript isn't like Java or C#, this. isn't optional.


In a comment you've asked:

wat if I can change the Sum function or anything inside the function but I can't change the var s1 = Sum();calling?

In that case, you'd make Sum a builder function and have it return an object, like this:

function Sum() {
return {
result: 0,
getCurrentSum: function getCurrentSum() {
return this.result;
},
add: function add(add_number) {
this.result += add_number;
}
};
}

Live Example:

(function () {
function Sum() { return { result: 0, getCurrentSum: function getCurrentSum() { return this.result; }, add: function add(add_number) { this.result += add_number; } }; }
var s1 = Sum(); // here s1 is undefined var s2 = Sum(); // here s2 is undefined
s1.add(10); // here cannot execute add of undefined crash s1.add(20);
s2.add(30); s2.add(5);
// must print 30 console.log(s1.getCurrentSum());
// must print 35 console.log(s2.getCurrentSum());})();

IF statement in MySQL as an Event

UPDATE core_members 
SET group_2 = ''
WHERE group_1 = 12
AND group_2 = 12

Unauthorized when fetching Tasks with Microsoft graph

Your token is scoped for Graph, not Outlook. Tasks.readwrite will default to the Microsoft Graph and won't work against the Outlook endpoint.

Change this bit:

this.userAgentApplication.loginPopup(['Tasks.readwrite'])

To:

this.userAgentApplication.loginPopup(['https://outlook.office.com/Tasks.readwrite'])

Javascript: Bind/unbind function behavior

The reason for this is issue is that calling bind() on a function returns a new instance of that function:

function someHandler() {  alert('hi');}
const someHandlerBinded = someHandler.bind(document);

// Returns false, seeing as these are different instances of the functionconsole.log( someHandlerBinded === someHandler );

flink cep sql Event Not triggering

There are several things that can cause pattern matching to produce no results:

  • the input doesn't actually contain the pattern
  • watermarking is being done incorrectly
  • the pattern is pathological in some way

This particular pattern loops with no exit condition. This sort of pattern doesn't allow the internal state of the pattern matching engine to ever be cleared, which will lead to problems.

If you were using Flink CEP directly, I would tell you to
try adding until(condition) or within(time) to constrain the number of possible matches.

With MATCH_RECOGNIZE, see if you can add a distinct terminating element to the pattern.


Update: since you are still getting no results after modifying the pattern, you should determine if watermarking is the source of your problem. CEP relies on sorting the input stream by time, which depends on watermarking -- but only if you are using event time.

The easiest way to test this would be to switch to using processing time:

create table agent_action_detail 
(
agent_id String,
...
row_time AS PROCTIME()
)
with (...)

If that works, then either the timestamps or watermarks are the problem. For example, if all of the events are late, you'll get no results. In your case, I'm wondering the row_time column has any data in it.


If that doesn't reveal the problem, please share a minimal reproducible example, including the data needed to observe the problem.

getting the basic socket.io sample to work

EADDRINUSE means that address is already in use so it can't get the socket. Is something else already running on port 80 on your machine? 80 is commonly used by web servers.

You can also try it on some other port.

The reason you see a blank file is it doesn't connect to the node server (since it couldn't get the socket) so the on news event will never get called. It might even connecting to the socket of whatever else is running on 80 which will never emit that event :)

After you solve the port conflict, when you run the server, it should just say:

info - socket.io started

and now it's waiting for connections.

Make sure you update the htm line to your port. For example, if 81:

var socket = io.connect('http://localhost:81'); // note the :81

EDIT:
I just tried it out and in the htm file I had to set the relative path to the socket.io.js file. After installing it via npm it was here in my directory.

<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>

Make sure that path is relative to the htm file (doesn't start with a /). Here's how I found out where mine was:

find . -name 'socket.io.js'

On Win:
dir socket.io.js /s

You should also run the host with (on *nix you may need sudo in front):

node host.js

Last thing I did when trying the sample was changing a couple lines in the htm file to this so I could see an alert message box when the event happened:

socket.on('news', function (data) {
alert(JSON.stringify(data));

Get id of parent div from clicked form submit

Just use the JQuery parent() method:

alert($(this).parent().attr('id'));

Also, as others have pointed out, you have a different issue in that this isn't pointing to the form when you use it in the success callback. You should cache that value and then use the cache variable. You can read more about how this works in JavaScript in another post of mine.

$(document).ready(function() {
$('form').submit(function () {
// Cache the object that "this" is bound to because
// in the success function, the invocation context
// changes and "this" won't point to the same object
// it does now.
var theForm = this;
$.ajax({
url: $(this).data('url'),
type: 'POST',
data: $(this).serialize(),
success: function (data) {
if (data !== "0") {
window.location.href = data;
} else {
//Just use the JQuery parent() method with the cached object
$(theForm).parent().attr('id')

}
},
error: function () {
alert("No idea what went wrong");
}
});
return false;
});
});


Related Topics



Leave a reply



Submit