0

setTimeout を使用して mousedown イベント中に call 関数を繰り返し使用したいのですが、まったく機能しません。誰でも何か考えがありますか?

    $("#image_360").mousedown( function() {
        mouseStillDown=true;
        $(this).data('timeout', window.setTimeout(function () {

            getDirection;
        }, 2000));
    },
    function()
    {
        clearTimeout($(this).data('timeout'));
        alert('mouse left');

    });

    function getDirection(e){
    var x = e.pageX;
            //do stuff with condition e.pageX
    }
4

1 に答える 1

3

タイマーの呼び出しとクリアを繰り返すには、 と の代わりに と メソッドをsetInterval()使用する必要があります。clearInterval()setTimeoutclearTimeout

setIntervalclearInterval

このようにコードを更新します

    $("#image_360").mousedown( function() {
        mouseStillDown=true;
        $(this).data('timeout', window.setInterval(function () {

            getDirection;
        }, 2000));
    },
    function()
    {
        clearInterval($(this).data('timeout'));
        alert('mouse left');

    });

その後getDirection、2 秒ごとに呼び出されます。

于 2012-05-15T19:40:48.550 に答える