Giter Site home page Giter Site logo

Comments (3)

 avatar commented on August 25, 2024

Hmm... I think the best way to do that currently is to define a global JS object with a method that calls the desired callback, then use the Duktape.proxy() method to create an object that can be called from Java. You could also use the evaluate method to invoke the callback directly: Duktape.evaluate("setTimeout(10)");

We don't provide a way to call a free (non-member) function from Java.

from zipline.

tyler-whitman avatar tyler-whitman commented on August 25, 2024

@ntrrgc did you end up finding a good way to implement setTimeout?

@szurbrigg would it be possible to provide a little more clarification on what you mean?

I currently exposed "setTimeout" as a global to JS which I have implemented as so:

public class SetTimeout implements ISetTimeout {

    private Handler handler;
    private HashMap<Integer, Runnable> timerMap;

    public SetTimeout(Handler handler, HashMap<Integer, Runnable> timerMap) {
        this.handler = handler;
        this.timerMap = timerMap;
    }

    @Override
    public int setTimeout(Object function, int delay) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                //need to callback the function here
            }
        };
        int id = task.hashCode();
        timerMap.put(id, task);
        handler.postDelayed(task, delay);
        return id;
    }
}

Then calling:
duktape.set("setTimeout", ISetTimeout.class, new SetTimeout(handler, timerMap));

Where ISetTimeout is defined as:
public interface ISetTimeout { int setTimeout(Object function, int delay); }

Obviously this crashes because of the function being passed as a parameter to the method. What is the recommended way around this?

from zipline.

ntrrgc avatar ntrrgc commented on August 25, 2024

@twhit093 No, I ended up rolling my own JavaScript bridge for Android that allows holding references to JS objects from Java (including functions) and therefore is capable of these things. https://github.com/ntrrgc/jsvm

from zipline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.